diff --git a/index.js b/index.js index 00a3d90..9753e87 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,21 @@ //loop array and extract the single item from the array per loop and store it in a variable //on each loop call iteratee() passing in the item from the current loop into the call to iteratee() //iteratee is a function that must return something, capture whatever it returns in a variable -//add the returned value from iteratee tp myNewArray +//add the returned value from iteratee to myNewArray //after looping, return myNewArray +const arr = [1, 2, 3, 4, 5]; + + function map(array, iteratee){ + const myNewArray = []; + + for(i=0; i < array.length; i++){ + let myVar = iteratee(arr[i]); + myNewArray.push(myVar); + } + + return myNewArray; } //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter @@ -24,8 +35,21 @@ function map(array, iteratee){ // passing in the item from the current loop //iteratee will return true or false, if true add the item to myNewArray else do not //after looping, return myNewArray +const arr = [1, 2, 3, 4, 5]; + function filter(array, iteratee){ + const myNewArray = []; + + for (let i = 0; i < array.length; i++) { + let index = array[i]; + if(iteratee(index)) { + myNewArray.push(index); + } + } + + return newArray; + } //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find @@ -34,8 +58,19 @@ function filter(array, iteratee){ // passing in the item from the current loop //fnc will return true or false, if true return the item //after looping, return null -function find(theArray, fnc){ +const arr = [1, 2, 3, 4, 5]; +function find(theArray, fnc){ + for(let i=0; i < array.length; i++){ + let index = array[i]; + if(fnc(index)) { + return true; + } else { + return false; + } + } + + return null; } @@ -45,24 +80,43 @@ function findLast(theArray){ } //return the first element of the array -function head(theArray){ +const arr = [1, 2, 3, 4, 5]; +function head(theArray){ + let index = arr[i]; + if(index === 0) { + return index; + } } //create a new array //loop theArray in reverse order //add the item from each loop to the new array //return the new array -function reverse(theArray){ +const arr = [1, 2, 3, 4, 5]; +function reverse(theArray){ + let myNewArray = []; + for (var i = arr.length - 1; i >= 0; i--) { + myNewArray.push(arr[i]); + } + return myNewArray; } //create a new array //loop theArray //add the item from each loop to the new array except the first item //return the new array +const arr = [1, 2, 3, 4, 5]; + function tail(theArray){ + myNewArray = []; + + for(i=1; i < arr.length; i++){ + myNewArray.push(arr); + } + return myNewArray; } //implement the most basic sorting algorithm there is @@ -86,4 +140,4 @@ exports.head = head; exports.reverse = reverse; exports.tail = tail; exports.sort = sort; -exports.findLast = findLast; +exports.findLast = findLast; \ No newline at end of file