From 8f948777d5f1aa70ad4157492df226e6093be969 Mon Sep 17 00:00:00 2001 From: Kyle Housel Date: Thu, 22 May 2025 14:56:56 -0400 Subject: [PATCH 1/3] Pass array method tests --- index.js | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/index.js b/index.js index e69de29bb..df291236a 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,122 @@ +//Collection Functions (Arrays or Objects) + +const myEach = function(collection, callback) { + if (Array.isArray(collection)) { + for (let i = 0; i < collection.length; i++) { + callback(collection[i]) + } + } else { + for (let key in collection) { + callback(collection[key]) + } + } + return collection +} + +const myMap = function(collection, callback) { + const mapArray = [] + + myEach(collection, (item, index, collection) => { + mapArray.push(callback(item, index, collection)) + }) + + return mapArray +} + +const myReduce = function(collection, callback, acc) { + + const objectValues = [] + + if (!Array.isArray(collection)) { + for (let key in collection) { + objectValues.push(collection[key]) + } + collection = objectValues + } + + if (!acc) { + acc = collection[0] + + myEach(collection.slice(1), (value) => { + acc = callback(acc, value) + }) + return acc + } + myEach(collection, (value) => { + acc = callback(acc, value) + }) + + return acc + +} + +const myFind = function(collection, predicate) { + for (let i = 0; i < collection.length; i++) { + if (predicate(collection[i])) { + return collection[i]; + } + } + return undefined +} + +const myFilter = function(collection, predicate) { + const filteredArray = [] + + for (let i = 0; i < collection.length; i++) { + if (predicate(collection[i])) { + filteredArray.push(collection[i]) + } + } + + return filteredArray +} + +const mySize = function(collection) { + + const objectKeys = [] + + if (!Array.isArray(collection)) { + for (let key in collection) { + objectKeys.push(key) + } + collection = objectKeys + } + + return collection.length +} + + +//Array Functions + +const myFirst = function (array, n) { + if (n) { + return array.slice(0, n) + } + return array[0] +} + +const myLast = function (array, n) { + + if (n) { + return array.slice(-n) + } + return array[mySize(array)-1] +} + +const mySortBy = function (array, callback) { + +} + +const myFlatten = function (array, [shallow], newArr=[]) { + +} + +//Object Functions + +const myKeys = object => { + +} + +const myValues = function (object) { + +} From 843be6055e26e8bdd61d1016395d1444c42beeb2 Mon Sep 17 00:00:00 2001 From: Kyle Housel Date: Thu, 22 May 2025 14:59:06 -0400 Subject: [PATCH 2/3] Pass object method tests --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index df291236a..f28233c16 100644 --- a/index.js +++ b/index.js @@ -114,9 +114,19 @@ const myFlatten = function (array, [shallow], newArr=[]) { //Object Functions const myKeys = object => { + const keys = [] + for (let key in object) { + keys.push(key) + } + return keys } const myValues = function (object) { + const values = [] + for (let key in object) { + values.push(object[key]) + } + return values } From faf711e541bac84be96a777d8a2d63221631039d Mon Sep 17 00:00:00 2001 From: Kyle Housel Date: Thu, 22 May 2025 15:39:09 -0400 Subject: [PATCH 3/3] Create mySortBy function with a disgusting amount of assistance --- index.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index f28233c16..95d51046c 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ //Collection Functions (Arrays or Objects) -const myEach = function(collection, callback) { +const myEach = (collection, callback) => { if (Array.isArray(collection)) { for (let i = 0; i < collection.length; i++) { callback(collection[i]) @@ -13,7 +13,7 @@ const myEach = function(collection, callback) { return collection } -const myMap = function(collection, callback) { +const myMap = (collection, callback) => { const mapArray = [] myEach(collection, (item, index, collection) => { @@ -23,7 +23,7 @@ const myMap = function(collection, callback) { return mapArray } -const myReduce = function(collection, callback, acc) { +const myReduce = (collection, callback, acc) => { const objectValues = [] @@ -50,7 +50,7 @@ const myReduce = function(collection, callback, acc) { } -const myFind = function(collection, predicate) { +const myFind = (collection, predicate) => { for (let i = 0; i < collection.length; i++) { if (predicate(collection[i])) { return collection[i]; @@ -59,7 +59,7 @@ const myFind = function(collection, predicate) { return undefined } -const myFilter = function(collection, predicate) { +const myFilter = (collection, predicate) => { const filteredArray = [] for (let i = 0; i < collection.length; i++) { @@ -71,7 +71,7 @@ const myFilter = function(collection, predicate) { return filteredArray } -const mySize = function(collection) { +const mySize = collection => { const objectKeys = [] @@ -88,14 +88,14 @@ const mySize = function(collection) { //Array Functions -const myFirst = function (array, n) { +const myFirst = (array, n) => { if (n) { return array.slice(0, n) } return array[0] } -const myLast = function (array, n) { +const myLast = (array, n) => { if (n) { return array.slice(-n) @@ -103,11 +103,20 @@ const myLast = function (array, n) { return array[mySize(array)-1] } -const mySortBy = function (array, callback) { +const mySortBy = (array, callback) => { + return [...array].sort((a, b) => { + const valA = callback(a); + const valB = callback(b); + if (typeof valA === 'string' && typeof valB === 'string') { + return valA.localeCompare(valB); + } + + return valA - valB; + }) } -const myFlatten = function (array, [shallow], newArr=[]) { +const myFlatten = (array, [shallow], newArr=[]) => { }