From ea66eb2534ad62890a885e9c717efffe670dff8b Mon Sep 17 00:00:00 2001 From: Yuriy Ivanenko Date: Tue, 21 May 2024 14:53:42 -0400 Subject: [PATCH 1/2] Create myEach Function --- index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/index.js b/index.js index e69de29bb..bc2cf24d7 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,22 @@ +const unmodifiedTestArr = [1, 2, 3, 4] +const unmodifiedTestObj = {one: 1, two: 2, three: 3, four: 4} + +const checkDataType = (collection) => Array.isArray(collection) ? 'array' : 'object' + +const myEach = (collection, callbackFunc) => { + checkDataType(collection) === 'array' ? + collection.forEach(callbackFunc) : + Object.values(collection).forEach(callbackFunc) + return collection +} + +const alertMe = (item) => { + alert(`${item}`) +} + +const myMap = (collection, callbackFunc) => { + checkDataType(collection) +} + + +console.log(myMap(unmodifiedTestArr, alertMe)) From 7805c79d8782fe450e47e229fe61a5749f073246 Mon Sep 17 00:00:00 2001 From: Yuriy Ivanenko Date: Wed, 22 May 2024 13:30:51 -0400 Subject: [PATCH 2/2] Pass tests --- index.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index bc2cf24d7..82870f707 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,94 @@ -const unmodifiedTestArr = [1, 2, 3, 4] -const unmodifiedTestObj = {one: 1, two: 2, three: 3, four: 4} - -const checkDataType = (collection) => Array.isArray(collection) ? 'array' : 'object' +const changeToArray = (collection) => Array.isArray(collection) ? collection : Object.values(collection) +const multiplyByThree = (num) => num * 3 +const alertMe = (item) => alert(`${item}`) +const callback = (acc, i, array) => (acc + (i * 3)) +const callback2 = (acc, val, collection) => (acc + val ) +function findCBGenerator(target) { + return (function(currEl) { return target === currEl }) +} const myEach = (collection, callbackFunc) => { - checkDataType(collection) === 'array' ? - collection.forEach(callbackFunc) : - Object.values(collection).forEach(callbackFunc) + changeToArray(collection).forEach(callbackFunc) return collection } -const alertMe = (item) => { - alert(`${item}`) +const myMap = (collection, callbackFunc) => changeToArray(collection).map(callbackFunc) + +const myReduce = (collection, callbackFunc, acc) => { + const array = changeToArray(collection) + let startIndex = 0 + if(!acc){ + acc = array[0] + startIndex = 1 + } + let total = acc + for(let i = startIndex; i < array.length; i++){ + acc = callbackFunc(acc, array[i],array) + } +return acc +} + + +const myFind = (collection, callBackFunc) => { + const array = changeToArray(collection) + for(let i = 0; i < array.length; i++){ + if(callBackFunc(collection[i])){ + return collection[i] + } + } + return +} + +function excluder(currEl) { + return (currEl > 10) } -const myMap = (collection, callbackFunc) => { - checkDataType(collection) +const myFilter = (collection, callBackFunc) => { + const array = changeToArray(collection) + let returnArray = [] + for(let i = 0; i < array.length; i++){ + if(callBackFunc(array[i])){ + returnArray.push(array[i]) + } + } + return returnArray } +const mySize = (collection) => { + const array = changeToArray(collection) + return array.length +} + +const myFirst = (collection, elemsToReturn) => { + const array = changeToArray(collection) + if(!elemsToReturn){ + return array[0] + }else{ + return array.splice(0,elemsToReturn) + } +} + +const myLast = (collection, elemsToReturn) => { + const array = changeToArray(collection) + if(!elemsToReturn){ + return array[array.length - 1] + }else{ + return array.splice(-elemsToReturn) + } +} + +const myKeys = (collection) => { + const returnKeys = [] + for(const key in collection)[ + returnKeys.push(key) + ] + return returnKeys +} -console.log(myMap(unmodifiedTestArr, alertMe)) +const myValues = (collection) => { + const returnValues = [] + for(const key in collection){ + returnValues.push(collection[key]) + } + return returnValues +}