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
64 changes: 59 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}


Expand All @@ -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
Expand All @@ -86,4 +140,4 @@ exports.head = head;
exports.reverse = reverse;
exports.tail = tail;
exports.sort = sort;
exports.findLast = findLast;
exports.findLast = findLast;