diff --git a/.results.json b/.results.json new file mode 100644 index 000000000..df9f38ac1 --- /dev/null +++ b/.results.json @@ -0,0 +1,396 @@ +{ + "stats": { + "suites": 11, + "tests": 27, + "passes": 27, + "pending": 0, + "failures": 0, + "start": "2024-06-04T18:20:31.097Z", + "end": "2024-06-04T18:20:31.210Z", + "duration": 113 + }, + "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": 1, + "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": 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": {} + }, + { + "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": 1, + "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": 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": {} + }, + { + "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..adaec63ab 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,96 @@ +const myEach = (collection, callBack) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + for (let i = 0; i < values.length; i++) { + callBack(values[i]) + } + return collection +} + +const myMap = (collection, callBack) => { + const newcollection = [] + const values = Array.isArray(collection) ? collection : Object.values(collection); + for (let i = 0; i < values.length; i++) { + newcollection.push(callBack(values[i])) + } + return (newcollection) +} + +const myReduce = (collection, callBack, acc) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + if (acc){ + let total = acc + for (let i = 0; i < values.length; i++) { + total = total + callBack(0, values[i], collection) + console.log(total) + } + return(total) + }else{ + let total = values[0] + const newValues = values.slice(1) + total = values[0] + for (let i = 0; i < newValues.length; i++) { + total = total + callBack(0, newValues[i], collection) + } + return total + } +} + +const myFind = (collection, callBack) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + for (let i = 0; i < values.length; i++) { + if(callBack(collection[i])){ + return collection[i] + } + } +} + +const myFilter = (collection, callBack) => { + const newCollection = [] + const values = Array.isArray(collection) ? collection : Object.values(collection); + for (let i = 0; i < values.length; i++) { + if(callBack(collection[i])){ + newCollection.push(collection[i]) + } + } + return newCollection +} + +const mySize = (collection) => { + const values = Array.isArray(collection) ? collection : Object.values(collection); + return values.length +} + +const myFirst = (collection, n) => { + if (n === undefined){ + return collection[0] + }else{ + return collection.slice(0, n) + } +} + +const myLast = (collection, n) => { + if (n === undefined){ + return collection[collection.length - 1] + }else{ + return collection.slice(-n) + } +} +const myKeys = (obj) => { + const keys = []; + for (let key in obj) { + if (obj.hasOwnProperty(key)) { + keys.push(key); + } + } + return keys; +} + +const myValues = (obj) => { + const values = []; + for (let key in obj) { + if (obj.hasOwnProperty(key)) { + values.push(obj[key]); + } + } + return values; +}