diff --git a/index.js b/index.js index e69de29bb..50c41aceb 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,169 @@ +function myEach(collection, callback) { + const origColl = collection + if (typeof collection === 'object') { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + callback(collection[i]) + } + + return origColl +} + +function myMap(collection, callback) { + const newColl = [] + if (typeof collection === "object") { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + newColl.push(callback(collection[i])) + } + + return newColl +} + +function myReduce(collection, callback, accrual) { + if (typeof collection === "object") { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + if(accrual != undefined){ + accrual = callback(accrual, collection[i]) + } else { + accrual = collection[i] + } + } + + return accrual +} + +function myFind(collection, predicate) { + if (typeof collection === "object") { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + if(predicate(collection[i])) { + return collection[i] + } + } +} + +function myFilter(collection, predicate) { + const newColl = [] + + if (typeof collection === "object") { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + if(predicate(collection[i])) { + newColl.push(collection[i]) + } + } + + return newColl +} + +function mySize(collection) { + let size = 0 + + if (typeof collection === "object") { + collection = Object.values(collection) + } + + for(let i = 0; i < collection.length; i++) { + size++ + } + + return size +} + +function myFirst(arr, count = 1) { + if (count === 1) { + return arr[0] + } else { + const newArr = [] + for (let i = 0; i < count; i++) { + newArr.push(arr[i]) + } + return newArr + } +} + +function myLast(arr, count = 1) { + if (count === 1) { + return arr[arr.length-1] + } else { + const newArr = [] + for (let i = arr.length - count; i < arr.length; i++) { + newArr.push(arr[i]) + } + return newArr + } +} + +function mySortBy(array, callback) { + const sortedArr = [array[0]] + + for (let i = 1; i < array.length; i++) { + for (let j = 0; j < sortedArr.length; j++) { + if (callback(array[i]) <= callback(sortedArr[j])) { + sortedArr.splice(j, 0, array[i]) + break + } else if (j === sortedArr.length - 1) { + sortedArr.push(array[i]) + break + } + } + } + return sortedArr +} + +function myFlatten(array, bleh, flattenedArr = []) { + if (bleh === true) { + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === "object") { + for (let j = 0; j < array[i].length; j++) { + flattenedArr.push(array[i][j]) + } + } else { + flattenedArr.push(array[i]) + } + } + return flattenedArr + } else { + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === "object") { + myFlatten(array[i], false, flattenedArr) + } else { + flattenedArr.push(array[i]) + } + } + + return flattenedArr + } +} + +function myKeys(obj) { + const keyArr = [] + + for(const key in obj) { + keyArr.push(key) + } + + return keyArr +} + +function myValues(obj) { + const valueArr = [] + + for(const key in obj) { + valueArr.push(obj[key]) + } + + return valueArr +} \ No newline at end of file