From 8e07c61f2cca28f1dfa162a4c8b6aa25d29d36e2 Mon Sep 17 00:00:00 2001 From: Cayla Horsey Date: Mon, 3 Jun 2024 11:57:08 -0400 Subject: [PATCH 1/3] Complete myMap, myEach, myFind --- index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/index.js b/index.js index e69de29bb..16f4ef15f 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,52 @@ +// + +const unmodifiedTestArr = [1, 2, 3, 4] +const unmodifiedTestObj = {one: 1, two: 2, three: 3, four: 4} +const testArr = [1, 2, 3, 4] + +const myEach = function(collection, callback) { + for(const item of Object.values(collection)) { + callback(item) + } + return collection +} + +// console.log(myEach(unmodifiedTestObj, alert)) + +const myMap = function (collection, callback) { + let newCollection = [] + for(const item of Object.values(collection)) { + newCollection.push(callback(item)) + } + return newCollection +} + +//COME BACK TO myReduce +// const myReduce = function(collection, callback, acc) { +// let singleValue = 0 + +// for (const item of Object.values(collection)) { +// singleValue = callback(item) +// } + +// return singleValue +// } + +// const callback = (acc, val, collection) => (acc + (val * 3)) + +// console.log(myReduce(testArr, callback, 10)) +const intArr = [-1, 4, 0, 1, 3, 2, 3, 4, 5, 6] + +function findCBGenerator(target) { + return (function(currEl) { return target === currEl }) +} + +const myFind = function(collection, predicate) { + for( const item of Object.values(collection)) { + if (predicate(item)) { + return item + } + } +} + +console.log(myFind(intArr, findCBGenerator(8))) From 8f54d6d3b6a6ad385ce4e47a278f8bb1b201aa19 Mon Sep 17 00:00:00 2001 From: Cayla Horsey Date: Mon, 3 Jun 2024 14:21:32 -0400 Subject: [PATCH 2/3] Complete all tests --- index.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 16f4ef15f..18e8b76b0 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,9 @@ const unmodifiedTestArr = [1, 2, 3, 4] const unmodifiedTestObj = {one: 1, two: 2, three: 3, four: 4} -const testArr = [1, 2, 3, 4] +// const testArr = [1, 2, 3, 4] +const testArr = unmodifiedTestArr.slice() + const myEach = function(collection, callback) { for(const item of Object.values(collection)) { @@ -21,7 +23,7 @@ const myMap = function (collection, callback) { return newCollection } -//COME BACK TO myReduce +// // const myReduce = function(collection, callback, acc) { // let singleValue = 0 @@ -35,6 +37,36 @@ const myMap = function (collection, callback) { // const callback = (acc, val, collection) => (acc + (val * 3)) // console.log(myReduce(testArr, callback, 10)) + + +const callback = (acc, val, collection) => (acc + (val * 3)) + + +const myReduce = function(collection, callback, acc) { + let singleValue + let startIndex + + if (acc === undefined) { + const values = Object.values(collection) + singleValue = values[0] + startIndex = 1 + } else { + singleValue = acc + startIndex = 0 + } + + const values = Object.values(collection) + for (let i = startIndex; i < values.length; i++) { + singleValue = callback(singleValue, values[i], collection) + } + + return singleValue +} + +console.log(myReduce(testArr, callback)) + +// + const intArr = [-1, 4, 0, 1, 3, 2, 3, 4, 5, 6] function findCBGenerator(target) { @@ -49,4 +81,69 @@ const myFind = function(collection, predicate) { } } -console.log(myFind(intArr, findCBGenerator(8))) +// console.log(myFind(intArr, findCBGenerator(8))) + + +const myFilter = function(collection, predicate) { + let newCollection = [] + for( const item of Object.values(collection)) { + if (predicate(item)) { + newCollection.push(item) + } + } return newCollection +} + +const mySize = function(collection) { + let numOfItems = 0 + for(let i = 0; i < Object.values(collection).length; i++){ + numOfItems += 1 + } + return numOfItems +} + +// console.log(mySize(intArr)) + +// const myFirst = function(array, n = 1) { +// return array.slice(0, n) +// } + +function myFirst(array, n) { + if (n === undefined) { + return array[0] + } else { + return array.slice(0, n); + } +} + +// console.log(myFirst(testArr)) +// console.log(myFirst(testArr, 3)) + + +function myLast(array, n) { + if (n === undefined) { + return array[array.length - 1] + } else { + return array.slice(-n) + } +} + +// console.log(myLast(testArr)) +// console.log(myLast(testArr, 3)) + +const myKeys = function(object) { + let keys = [] + for(const item in object) { + keys.push(item) + } + return keys +} + +const myValues = function(object) { + let values = [] + for(const item in object) { + values.push(object[item]) + } + return values +} + +// console.log(myValues(unmodifiedTestObj)) From 0330597771c09ecdeb2876ad80cb37ca1287471d Mon Sep 17 00:00:00 2001 From: Cayla Horsey Date: Mon, 3 Jun 2024 14:34:07 -0400 Subject: [PATCH 3/3] Refactor code --- index.js | 63 +++----------------------------------------------------- 1 file changed, 3 insertions(+), 60 deletions(-) diff --git a/index.js b/index.js index 18e8b76b0..6c9c1ada6 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,3 @@ -// - -const unmodifiedTestArr = [1, 2, 3, 4] -const unmodifiedTestObj = {one: 1, two: 2, three: 3, four: 4} -// const testArr = [1, 2, 3, 4] -const testArr = unmodifiedTestArr.slice() - - const myEach = function(collection, callback) { for(const item of Object.values(collection)) { callback(item) @@ -13,8 +5,6 @@ const myEach = function(collection, callback) { return collection } -// console.log(myEach(unmodifiedTestObj, alert)) - const myMap = function (collection, callback) { let newCollection = [] for(const item of Object.values(collection)) { @@ -23,25 +13,6 @@ const myMap = function (collection, callback) { return newCollection } -// -// const myReduce = function(collection, callback, acc) { -// let singleValue = 0 - -// for (const item of Object.values(collection)) { -// singleValue = callback(item) -// } - -// return singleValue -// } - -// const callback = (acc, val, collection) => (acc + (val * 3)) - -// console.log(myReduce(testArr, callback, 10)) - - -const callback = (acc, val, collection) => (acc + (val * 3)) - - const myReduce = function(collection, callback, acc) { let singleValue let startIndex @@ -63,16 +34,6 @@ const myReduce = function(collection, callback, acc) { return singleValue } -console.log(myReduce(testArr, callback)) - -// - -const intArr = [-1, 4, 0, 1, 3, 2, 3, 4, 5, 6] - -function findCBGenerator(target) { - return (function(currEl) { return target === currEl }) -} - const myFind = function(collection, predicate) { for( const item of Object.values(collection)) { if (predicate(item)) { @@ -81,9 +42,6 @@ const myFind = function(collection, predicate) { } } -// console.log(myFind(intArr, findCBGenerator(8))) - - const myFilter = function(collection, predicate) { let newCollection = [] for( const item of Object.values(collection)) { @@ -101,25 +59,15 @@ const mySize = function(collection) { return numOfItems } -// console.log(mySize(intArr)) - -// const myFirst = function(array, n = 1) { -// return array.slice(0, n) -// } - -function myFirst(array, n) { +const myFirst = function(array, n) { if (n === undefined) { return array[0] } else { - return array.slice(0, n); + return array.slice(0, n) } } -// console.log(myFirst(testArr)) -// console.log(myFirst(testArr, 3)) - - -function myLast(array, n) { +const myLast = function(array, n) { if (n === undefined) { return array[array.length - 1] } else { @@ -127,9 +75,6 @@ function myLast(array, n) { } } -// console.log(myLast(testArr)) -// console.log(myLast(testArr, 3)) - const myKeys = function(object) { let keys = [] for(const item in object) { @@ -145,5 +90,3 @@ const myValues = function(object) { } return values } - -// console.log(myValues(unmodifiedTestObj))