From 76609230f72d62401f8b97131358cda65dddb89f Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 07:47:18 -0400 Subject: [PATCH 1/6] Create myEach and myMap functions --- .results.json | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 28 +++++++++++ package.json | 2 +- 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 .results.json diff --git a/.results.json b/.results.json new file mode 100644 index 000000000..ee0b5b0a2 --- /dev/null +++ b/.results.json @@ -0,0 +1,127 @@ +{ + "stats": { + "suites": 4, + "tests": 7, + "passes": 7, + "pending": 0, + "failures": 1, + "start": "2025-05-23T11:46:15.227Z", + "end": "2025-05-23T11:46:15.329Z", + "duration": 102 + }, + "tests": [ + { + "title": "calls alert with each element passed", + "fullTitle": "index.js myEach calls alert with each element passed", + "duration": 1, + "currentRetry": 0, + "err": {} + }, + { + "title": "calls alert properly on object values", + "fullTitle": "index.js myEach calls alert properly on object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the original collection", + "fullTitle": "index.js myEach returns the original collection", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "successfully returns a correctly populated array", + "fullTitle": "index.js myMap successfully returns a correctly populated array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original array", + "fullTitle": "index.js myMap does not modify the original array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "successfully returns a correctly populated array from modified object values", + "fullTitle": "index.js myMap successfully returns a correctly populated array from modified object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myMap does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} + } + ], + "pending": [], + "failures": [ + { + "title": "returns the correct reduced value when passed an initial value", + "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", + "duration": 0, + "currentRetry": 0, + "err": { + "stack": "ReferenceError: myReduce is not defined\n at Context. (test/indexTest.js:71:29)\n at process.processImmediate (node:internal/timers:478:21)", + "message": "myReduce is not defined" + } + } + ], + "passes": [ + { + "title": "calls alert with each element passed", + "fullTitle": "index.js myEach calls alert with each element passed", + "duration": 1, + "currentRetry": 0, + "err": {} + }, + { + "title": "calls alert properly on object values", + "fullTitle": "index.js myEach calls alert properly on object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the original collection", + "fullTitle": "index.js myEach returns the original collection", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "successfully returns a correctly populated array", + "fullTitle": "index.js myMap successfully returns a correctly populated array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original array", + "fullTitle": "index.js myMap does not modify the original array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "successfully returns a correctly populated array from modified object values", + "fullTitle": "index.js myMap successfully returns a correctly populated array from modified object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myMap does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js index e69de29bb..d96edc6e7 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,28 @@ +const myEach = (collection, callback) => { + if (Array.isArray(collection)) { + collection.forEach(item => { + callback(item); + }); + } else { + for (let key in collection) { + callback(collection[key]); + } + } + return collection; +}; + +const myMap = (collection, callback) => { + const newArray = []; + + if (Array.isArray(collection)) { + collection.forEach(function (value, index) { + newArray.push(callback(value, index)); + }); + } else { + Object.keys(collection).forEach(function (key) { + newArray.push(callback(collection[key], key)); + }); + } + + return newArray; +}; diff --git a/package.json b/package.json index 81c96dcdb..08185a726 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 -R mocha-multi --reporter-options spec=-,json=.results.json -b" }, "repository": { "type": "git", From 036dcfb44dc65464bb07fe56ec733ba9c5096bc1 Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 08:45:39 -0400 Subject: [PATCH 2/6] Create myReduce function --- .results.json | 90 +++++++++++++++++++++++++++++++++++++++++++++------ index.js | 28 ++++++++++++++++ 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/.results.json b/.results.json index ee0b5b0a2..f73b6f2d2 100644 --- a/.results.json +++ b/.results.json @@ -1,13 +1,13 @@ { "stats": { - "suites": 4, - "tests": 7, - "passes": 7, + "suites": 5, + "tests": 12, + "passes": 12, "pending": 0, "failures": 1, - "start": "2025-05-23T11:46:15.227Z", - "end": "2025-05-23T11:46:15.329Z", - "duration": 102 + "start": "2025-05-23T12:08:10.704Z", + "end": "2025-05-23T12:08:10.808Z", + "duration": 104 }, "tests": [ { @@ -58,18 +58,53 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "returns the correct reduced value when passed an initial value", + "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", + "duration": 1, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the correct reduced value when not passed an initial value", + "fullTitle": "index.js myReduce returns the correct reduced value when not passed an initial value", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original array", + "fullTitle": "index.js myReduce does not modify the original array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the correct reduced value from object values", + "fullTitle": "index.js myReduce returns the correct reduced value from object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myReduce does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} } ], "pending": [], "failures": [ { - "title": "returns the correct reduced value when passed an initial value", - "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", + "title": "returns the value if found", + "fullTitle": "index.js myFind returns the value if found", "duration": 0, "currentRetry": 0, "err": { - "stack": "ReferenceError: myReduce is not defined\n at Context. (test/indexTest.js:71:29)\n at process.processImmediate (node:internal/timers:478:21)", - "message": "myReduce is not defined" + "stack": "ReferenceError: myFind is not defined\n at Context. (test/indexTest.js:106:7)\n at process.processImmediate (node:internal/timers:478:21)", + "message": "myFind is not defined" } } ], @@ -122,6 +157,41 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "returns the correct reduced value when passed an initial value", + "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", + "duration": 1, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the correct reduced value when not passed an initial value", + "fullTitle": "index.js myReduce returns the correct reduced value when not passed an initial value", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original array", + "fullTitle": "index.js myReduce does not modify the original array", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the correct reduced value from object values", + "fullTitle": "index.js myReduce returns the correct reduced value from object values", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myReduce does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} } ] } \ No newline at end of file diff --git a/index.js b/index.js index d96edc6e7..d5fe7d9e2 100644 --- a/index.js +++ b/index.js @@ -26,3 +26,31 @@ const myMap = (collection, callback) => { return newArray; }; + +const myReduce = (collection, callback, acc) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + + if (acc !== undefined) { + return values.reduce(function (accumulator, currentValue) { + return callback(accumulator, currentValue, collection); + }, acc); + } else { + return values.reduce(function (accumulator, currentValue) { + return callback(accumulator, currentValue, collection); + }); + } +}; + +// const myReduce = (collection, callback, acc) => { +// let values = Array.isArray(collection) ? collection : Object.values(collection); +// let startIndex = 0; + +// if (acc === undefined) { +// acc = values[0]; *USE REDUCE HERE* +// startIndex = 1; +// } +// for (let i = startIndex; i < values.length; i++) { +// acc = callback(acc, values[i], collection); +// } +// return acc; +// }; From edf175ba8e2b2e97ace27122aacdc312c83ed39d Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 09:34:05 -0400 Subject: [PATCH 3/6] Create myFind function and update myMap to use map method --- .results.json | 66 +++++++++++++++++++++++++++++++++++++++++---------- index.js | 31 ++++++++---------------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/.results.json b/.results.json index f73b6f2d2..f26cb68ab 100644 --- a/.results.json +++ b/.results.json @@ -1,13 +1,13 @@ { "stats": { - "suites": 5, - "tests": 12, - "passes": 12, + "suites": 6, + "tests": 15, + "passes": 15, "pending": 0, "failures": 1, - "start": "2025-05-23T12:08:10.704Z", - "end": "2025-05-23T12:08:10.808Z", - "duration": 104 + "start": "2025-05-23T13:33:26.603Z", + "end": "2025-05-23T13:33:26.791Z", + "duration": 188 }, "tests": [ { @@ -62,7 +62,7 @@ { "title": "returns the correct reduced value when passed an initial value", "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -93,18 +93,39 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "returns the value if found", + "fullTitle": "index.js myFind returns the value if found", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not traverse the whole array if the value is found early", + "fullTitle": "index.js myFind does not traverse the whole array if the value is found early", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns undefined if the value is not present", + "fullTitle": "index.js myFind returns undefined if the value is not present", + "duration": 0, + "currentRetry": 0, + "err": {} } ], "pending": [], "failures": [ { - "title": "returns the value if found", - "fullTitle": "index.js myFind returns the value if found", + "title": "correctly filters for values that the callback evaluates as true", + "fullTitle": "index.js myFilter correctly filters for values that the callback evaluates as true", "duration": 0, "currentRetry": 0, "err": { - "stack": "ReferenceError: myFind is not defined\n at Context. (test/indexTest.js:106:7)\n at process.processImmediate (node:internal/timers:478:21)", - "message": "myFind is not defined" + "stack": "ReferenceError: myFilter is not defined\n at Context. (test/indexTest.js:134:29)\n at process.processImmediate (node:internal/timers:478:21)", + "message": "myFilter is not defined" } } ], @@ -161,7 +182,7 @@ { "title": "returns the correct reduced value when passed an initial value", "fullTitle": "index.js myReduce returns the correct reduced value when passed an initial value", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -192,6 +213,27 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "returns the value if found", + "fullTitle": "index.js myFind returns the value if found", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not traverse the whole array if the value is found early", + "fullTitle": "index.js myFind does not traverse the whole array if the value is found early", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns undefined if the value is not present", + "fullTitle": "index.js myFind returns undefined if the value is not present", + "duration": 0, + "currentRetry": 0, + "err": {} } ] } \ No newline at end of file diff --git a/index.js b/index.js index d5fe7d9e2..cecc890d5 100644 --- a/index.js +++ b/index.js @@ -12,19 +12,15 @@ const myEach = (collection, callback) => { }; const myMap = (collection, callback) => { - const newArray = []; - if (Array.isArray(collection)) { - collection.forEach(function (value, index) { - newArray.push(callback(value, index)); + return collection.map(function (value, index) { + return callback(value, index); }); } else { - Object.keys(collection).forEach(function (key) { - newArray.push(callback(collection[key], key)); + return Object.keys(collection).map(function (key) { + return callback(collection[key], key); }); } - - return newArray; }; const myReduce = (collection, callback, acc) => { @@ -41,16 +37,9 @@ const myReduce = (collection, callback, acc) => { } }; -// const myReduce = (collection, callback, acc) => { -// let values = Array.isArray(collection) ? collection : Object.values(collection); -// let startIndex = 0; - -// if (acc === undefined) { -// acc = values[0]; *USE REDUCE HERE* -// startIndex = 1; -// } -// for (let i = startIndex; i < values.length; i++) { -// acc = callback(acc, values[i], collection); -// } -// return acc; -// }; +const myFind = (collection, predicate) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + return values.find(function (value) { + return predicate(value); + }); +}; From b8cd89ef5c884f8f255dd7311342a347704c0ef9 Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 09:45:57 -0400 Subject: [PATCH 4/6] Create myFiler and mySize functions --- .results.json | 84 ++++++++++++++++++++++++++++++++++++++++++--------- index.js | 12 ++++++++ 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/.results.json b/.results.json index f26cb68ab..6f6bde79c 100644 --- a/.results.json +++ b/.results.json @@ -1,19 +1,19 @@ { "stats": { - "suites": 6, - "tests": 15, - "passes": 15, + "suites": 8, + "tests": 19, + "passes": 19, "pending": 0, "failures": 1, - "start": "2025-05-23T13:33:26.603Z", - "end": "2025-05-23T13:33:26.791Z", - "duration": 188 + "start": "2025-05-23T13:41:53.659Z", + "end": "2025-05-23T13:41:53.830Z", + "duration": 171 }, "tests": [ { "title": "calls alert with each element passed", "fullTitle": "index.js myEach calls alert with each element passed", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -55,7 +55,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myMap does not modify the original object", - "duration": 0, + "duration": 1, "currentRetry": 0, "err": {} }, @@ -114,18 +114,46 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "correctly filters for values that the callback evaluates as true", + "fullTitle": "index.js myFilter correctly filters for values that the callback evaluates as true", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns an empty array if no matching values are found", + "fullTitle": "index.js myFilter correctly returns an empty array if no matching values are found", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns the size of the collection when an array is passed", + "fullTitle": "index.js mySize correctly returns the size of the collection when an array is passed", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns the size of the collection (amount of keys) when an object is passed", + "fullTitle": "index.js mySize correctly returns the size of the collection (amount of keys) when an object is passed", + "duration": 0, + "currentRetry": 0, + "err": {} } ], "pending": [], "failures": [ { - "title": "correctly filters for values that the callback evaluates as true", - "fullTitle": "index.js myFilter correctly filters for values that the callback evaluates as true", + "title": "returns the first element of the collection", + "fullTitle": "index.js myFirst returns the first element of the collection", "duration": 0, "currentRetry": 0, "err": { - "stack": "ReferenceError: myFilter is not defined\n at Context. (test/indexTest.js:134:29)\n at process.processImmediate (node:internal/timers:478:21)", - "message": "myFilter is not defined" + "stack": "ReferenceError: myFirst is not defined\n at Context. (test/indexTest.js:161:7)\n at process.processImmediate (node:internal/timers:478:21)", + "message": "myFirst is not defined" } } ], @@ -133,7 +161,7 @@ { "title": "calls alert with each element passed", "fullTitle": "index.js myEach calls alert with each element passed", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -175,7 +203,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myMap does not modify the original object", - "duration": 0, + "duration": 1, "currentRetry": 0, "err": {} }, @@ -234,6 +262,34 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "correctly filters for values that the callback evaluates as true", + "fullTitle": "index.js myFilter correctly filters for values that the callback evaluates as true", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns an empty array if no matching values are found", + "fullTitle": "index.js myFilter correctly returns an empty array if no matching values are found", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns the size of the collection when an array is passed", + "fullTitle": "index.js mySize correctly returns the size of the collection when an array is passed", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "correctly returns the size of the collection (amount of keys) when an object is passed", + "fullTitle": "index.js mySize correctly returns the size of the collection (amount of keys) when an object is passed", + "duration": 0, + "currentRetry": 0, + "err": {} } ] } \ No newline at end of file diff --git a/index.js b/index.js index cecc890d5..2e7bd5004 100644 --- a/index.js +++ b/index.js @@ -43,3 +43,15 @@ const myFind = (collection, predicate) => { return predicate(value); }); }; + +const myFilter = (collection, predicate) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + return values.filter(function (value) { + return predicate(value); + }); +}; + +const mySize = (collection) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + return values.length; +} From 4787af4170ad2853959220e8f57c8113d60f557c Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 10:08:36 -0400 Subject: [PATCH 5/6] Create myFirst and myLast functions --- index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/index.js b/index.js index 2e7bd5004..5038f0896 100644 --- a/index.js +++ b/index.js @@ -55,3 +55,19 @@ const mySize = (collection) => { const values = Array.isArray(collection) ? collection : Object.values(collection); return values.length; } + +const myFirst = (array, n) => { + if (n === undefined) { + return array[0]; + } else { + return array.slice(0, n); + } +}; + +const myLast = (array, n) => { + if (n === undefined) { + return array[0]; + } else { + return array.slice(0, n); + } +}; From cf7f822df18a82b29727bc0a1b1280e52536328e Mon Sep 17 00:00:00 2001 From: Nihar Patel Date: Fri, 23 May 2025 10:14:31 -0400 Subject: [PATCH 6/6] Complete lab - Create myKeys and MyValues functions --- .results.json | 139 +++++++++++++++++++++++++++++++++++++++++++------- index.js | 12 ++++- 2 files changed, 130 insertions(+), 21 deletions(-) diff --git a/.results.json b/.results.json index 6f6bde79c..4b5cfb8ee 100644 --- a/.results.json +++ b/.results.json @@ -1,13 +1,13 @@ { "stats": { - "suites": 8, - "tests": 19, - "passes": 19, + "suites": 11, + "tests": 27, + "passes": 27, "pending": 0, - "failures": 1, - "start": "2025-05-23T13:41:53.659Z", - "end": "2025-05-23T13:41:53.830Z", - "duration": 171 + "failures": 0, + "start": "2025-05-23T14:13:32.484Z", + "end": "2025-05-23T14:13:32.592Z", + "duration": 108 }, "tests": [ { @@ -55,7 +55,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myMap does not modify the original object", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -90,7 +90,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myReduce does not modify the original object", - "duration": 0, + "duration": 1, "currentRetry": 0, "err": {} }, @@ -142,21 +142,66 @@ "duration": 0, "currentRetry": 0, "err": {} - } - ], - "pending": [], - "failures": [ + }, { "title": "returns the first element of the collection", "fullTitle": "index.js myFirst returns the first element of the collection", "duration": 0, "currentRetry": 0, - "err": { - "stack": "ReferenceError: myFirst is not defined\n at Context. (test/indexTest.js:161:7)\n at process.processImmediate (node:internal/timers:478:21)", - "message": "myFirst is not defined" - } + "err": {} + }, + { + "title": "returns the first n elements of the collection when the second optional argument (n) is provided", + "fullTitle": "index.js myFirst returns the first n elements of the collection when the second optional argument (n) is provided", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the last element of the collection", + "fullTitle": "index.js myLast returns the last element of the collection", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the last n elements of the collection when the second optional argument (n) is provided", + "fullTitle": "index.js myLast returns the last n elements of the collection when the second optional argument (n) is provided", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "retrieves all the names of the object's own enumerable properties", + "fullTitle": "index.js myKeys retrieves all the names of the object's own enumerable properties", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myKeys does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "retrieves all the values of the object's own properties", + "fullTitle": "index.js myValues retrieves all the values of the object's own properties", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myValues does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} } ], + "pending": [], + "failures": [], "passes": [ { "title": "calls alert with each element passed", @@ -203,7 +248,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myMap does not modify the original object", - "duration": 1, + "duration": 0, "currentRetry": 0, "err": {} }, @@ -238,7 +283,7 @@ { "title": "does not modify the original object", "fullTitle": "index.js myReduce does not modify the original object", - "duration": 0, + "duration": 1, "currentRetry": 0, "err": {} }, @@ -290,6 +335,62 @@ "duration": 0, "currentRetry": 0, "err": {} + }, + { + "title": "returns the first element of the collection", + "fullTitle": "index.js myFirst returns the first element of the collection", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the first n elements of the collection when the second optional argument (n) is provided", + "fullTitle": "index.js myFirst returns the first n elements of the collection when the second optional argument (n) is provided", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the last element of the collection", + "fullTitle": "index.js myLast returns the last element of the collection", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "returns the last n elements of the collection when the second optional argument (n) is provided", + "fullTitle": "index.js myLast returns the last n elements of the collection when the second optional argument (n) is provided", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "retrieves all the names of the object's own enumerable properties", + "fullTitle": "index.js myKeys retrieves all the names of the object's own enumerable properties", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myKeys does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "retrieves all the values of the object's own properties", + "fullTitle": "index.js myValues retrieves all the values of the object's own properties", + "duration": 0, + "currentRetry": 0, + "err": {} + }, + { + "title": "does not modify the original object", + "fullTitle": "index.js myValues does not modify the original object", + "duration": 0, + "currentRetry": 0, + "err": {} } ] } \ No newline at end of file diff --git a/index.js b/index.js index 5038f0896..7fb0b0480 100644 --- a/index.js +++ b/index.js @@ -66,8 +66,16 @@ const myFirst = (array, n) => { const myLast = (array, n) => { if (n === undefined) { - return array[0]; + return array[array.length - 1]; } else { - return array.slice(0, n); + return array.slice(-n); } }; + +const myKeys = (object) => { + return Object.keys(object); +}; + +const myValues = (object) => { + return Object.values(object); +};