diff --git a/.results.json b/.results.json new file mode 100644 index 000000000..4b5cfb8ee --- /dev/null +++ b/.results.json @@ -0,0 +1,396 @@ +{ + "stats": { + "suites": 11, + "tests": 27, + "passes": 27, + "pending": 0, + "failures": 0, + "start": "2025-05-23T14:13:32.484Z", + "end": "2025-05-23T14:13:32.592Z", + "duration": 108 + }, + "tests": [ + { + "title": "calls alert with each element passed", + "fullTitle": "index.js myEach calls alert with each element passed", + "duration": 0, + "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": {} + }, + { + "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": {} + }, + { + "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": 1, + "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": {} + }, + { + "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": {} + }, + { + "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": {} + } + ], + "pending": [], + "failures": [], + "passes": [ + { + "title": "calls alert with each element passed", + "fullTitle": "index.js myEach calls alert with each element passed", + "duration": 0, + "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": {} + }, + { + "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": {} + }, + { + "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": 1, + "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": {} + }, + { + "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": {} + }, + { + "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 e69de29bb..7fb0b0480 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,81 @@ +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) => { + if (Array.isArray(collection)) { + return collection.map(function (value, index) { + return callback(value, index); + }); + } else { + return Object.keys(collection).map(function (key) { + return callback(collection[key], key); + }); + } +}; + +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 myFind = (collection, predicate) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + return values.find(function (value) { + 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; +} + +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[array.length - 1]; + } else { + return array.slice(-n); + } +}; + +const myKeys = (object) => { + return Object.keys(object); +}; + +const myValues = (object) => { + return Object.values(object); +}; 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",