From 86ecab936d22be158739d6957076831a4232aecc Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 13:47:05 -0400 Subject: [PATCH 1/8] Implement myEach --- index.js | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e69de29bb..f2a216479 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,4 @@ +const myEach = (collection, callback) => { + Object.values(collection).forEach(callback) + return collection +} diff --git a/package.json b/package.json index 81c96dcdb..4c78dfd4e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "JavaScript Variables Lab for Learn.co", "main": "index.js", "scripts": { - "test": "mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json" + "test": "mocha --timeout 5000 -b -R mocha-multi --reporter-options spec=-,json=.results.json" }, "repository": { "type": "git", From 3e507d43cea1aca36107ccd8870d7a2df2530d19 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 13:55:56 -0400 Subject: [PATCH 2/8] Implement myMap --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index f2a216479..243f0f290 100644 --- a/index.js +++ b/index.js @@ -2,3 +2,11 @@ const myEach = (collection, callback) => { Object.values(collection).forEach(callback) return collection } + +const myMap = (collection, callback) => { + const returnArr = []; + Object.values(collection).forEach((value) => { + returnArr.push(callback(value)) + }) + return returnArr +} From 1d6f201f546a122cfc2f63b78d0bdacd00d37245 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 14:31:01 -0400 Subject: [PATCH 3/8] Implement myReduce --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index 243f0f290..b7f6c3d10 100644 --- a/index.js +++ b/index.js @@ -10,3 +10,15 @@ const myMap = (collection, callback) => { }) return returnArr } + +const myReduce = (collection, callback, accum) => { + let accumulator = accum; + Object.values(collection).forEach((value, index) => { + if (index === 0 && accumulator === undefined) { + accumulator = value; + } else { + accumulator = callback(accumulator, value, Object.values(collection)) + } + }) + return accumulator +} From 91a2061c7d32008957736956a313a7758d06859e Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 14:40:59 -0400 Subject: [PATCH 4/8] Implement myFind --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index b7f6c3d10..d498ea0d5 100644 --- a/index.js +++ b/index.js @@ -22,3 +22,13 @@ const myReduce = (collection, callback, accum) => { }) return accumulator } + +const myFind = (collection, predicate) => { + const values = Object.values(collection); + for (let i = 0; i < values.length; i++) { + if (predicate(values[i])) { + return values[i]; + } + } + return undefined; +} From 1c2cab2a489b1ed217c4be4fd6786997f20df309 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 14:42:43 -0400 Subject: [PATCH 5/8] Implement myFilter --- index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.js b/index.js index d498ea0d5..0700823da 100644 --- a/index.js +++ b/index.js @@ -32,3 +32,14 @@ const myFind = (collection, predicate) => { } return undefined; } + +const myFilter = (collection, predicate) => { + const values = Object.values(collection); + const returnValues = []; + for (let i = 0; i < values.length; i++) { + if (predicate(values[i])) { + returnValues.push(values[i]) + } + } + return returnValues; +} From 853ce7d577d143f0239025d6dcde5535e39273f2 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 14:44:20 -0400 Subject: [PATCH 6/8] Implement mySize --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 0700823da..8ebe65fa2 100644 --- a/index.js +++ b/index.js @@ -43,3 +43,9 @@ const myFilter = (collection, predicate) => { } return returnValues; } + +const mySize = (collection) => { + let count = 0; + Object.values(collection).forEach((_) => count += 1) + return count; +} From ec7d08923ccfc9487de6ba8834f5e78ee53d5bff Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 15:00:09 -0400 Subject: [PATCH 7/8] Implement myFirst and myLast --- index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/index.js b/index.js index 8ebe65fa2..1509d88fc 100644 --- a/index.js +++ b/index.js @@ -49,3 +49,19 @@ const mySize = (collection) => { Object.values(collection).forEach((_) => count += 1) return count; } + +const myFirst = (collection, n = 1) => { + const values = Object.values(collection); + const returnValues = []; + for (let i = 0; i < n; i++) { + returnValues.push(values[i]) + } + return returnValues.length > 1 ? returnValues : returnValues[0]; +} + +const myLast = (collection, n = 1) => { + debugger + let result = myFirst(Object.values(collection).reverse(), n) + if (typeof result !== 'number') result = result.reverse() + return result +} From dca8b4fba1aa4b96de5d5b17919865d6d9f3c2bd Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 15:12:01 -0400 Subject: [PATCH 8/8] Implement myKeys and myValues --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 1509d88fc..092227063 100644 --- a/index.js +++ b/index.js @@ -65,3 +65,17 @@ const myLast = (collection, n = 1) => { if (typeof result !== 'number') result = result.reverse() return result } + +const myKeys = (object) => { + const keys = []; + for (let key in object) { + keys.push(key); + } + return keys; +} + +const myValues = (object) => { + const values = []; + myKeys(object).forEach(value => values.push(object[value])) + return values +}