Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -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= [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for find isn't quite correct. Try to take the comment instructions a little more literally.
You don't need to create a new array "myArray". Its not necessary to how find works.
if the if statement is true, just return the item from the array, no need to push it to another array.

for (let i = 0; i < theArray.length; i++){
if(fnc(theArray[i])){
myArray.push(theArray[i]);
};
};
return myArray
}
}


Expand Down Expand Up @@ -65,4 +83,4 @@ export function tail(theArray){
//if false return theArray
export function sort(theArray){

}
}