diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..aef8efa 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,29 +1,47 @@ //in the function map, create a new array and store in a variable -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop into the call to fnc //add the returned value from fnc to the new array //return the new array export function map(theArray, fnc){ - + let myArray = []; + for(let i = 0;i < theArray.length;i ++ ){ + let aThing = fnc(theArray[i]); + myArray.push(aThing); + } + return myArray; } //create a new array -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop //fnc will return true or false, if true add the item to the new array else do not //return the new array export function filter(theArray, fnc){ - + let myArray = []; + for(let i = 0;i < theArray.length;i ++ ){ + if (fnc(theArray[i])){ + myArray.push(theArray[i]); + }; +}; + return newArray; } -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop -//fnc will return true or false, if true return the item +//fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + let myArray= []; + for (let i = 0; i < theArray.length; i++){ + if(fnc(theArray[i])){ + myArray.push(theArray[i]); + }; +}; + return myArray + } } @@ -65,4 +83,4 @@ export function tail(theArray){ //if false return theArray export function sort(theArray){ -} \ No newline at end of file +}