Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
package.json
92 changes: 87 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,25 @@
//iteratee is a function that must return something, capture whatever it returns in a variable
//add the returned value from iteratee tp myNewArray
//after looping, return myNewArray

let array1 = [11,3,5,8,4];
console.log(array1);
function map(array, iteratee){
let newArr = [];
for(let i=0; i<array.length; i++){
newArr.push(iteratee(array[i]));
}
console.log(newArr);
return newArr;
}

//adds 5 to an item
function iteratee1(item){
return item + 5;
}

map(array1, iteratee1); //output: [16,8,10,13,9]

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
//create a function called `filter`, it should take 2 parameters `array` and `iteratee`
//`array` must be an array
Expand All @@ -24,47 +39,97 @@ 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

function filter(array, iteratee){
let newArr = [];
for(let i=0; i<array.length; i++){
if(iteratee2(array[i])){
newArr.push(array[i]);
}
}
console.log(newArr);
return newArr;
}

//function returns true if item is greater than 5
function iteratee2(item){
return item > 5;
}

filter(array1, iteratee2); //output: [11,8]
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
//create a function called `find`, it should take 2 parameters `theArray` and `fnc`
//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
//after looping, return null
function find(theArray, fnc){
for(let i=0; i<theArray.length; i++){
if(fnc(theArray[i])){
let firstItem = theArray[i];
console.log(firstItem);
return firstItem;
}

}
}

//function returns true if item is less than 5
function fnc1(item){
return item < 5;
}

find(array1, fnc1); //output: 3


//return the last item in theArray
function findLast(theArray){
console.log(theArray[theArray.length-1]);
return theArray[theArray.length-1];

}
findLast(array1); //output: 4

//return the first element of the array
function head(theArray){

//return the first element of an array
function head(theArray){
console.log(theArray[0]);
return theArray[0];
}
head(array1); //output: 11


//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){

let newArr = [];
for(i = theArray.length-1; i>=0; i--){
newArr.push(theArray[i]);
}
console.log(newArr);
return newArr;
}

reverse(array1); //output: [4,8,5,3,11]


//create a new array
//loop theArray
//add the item from each loop to the new array except the first item
//return the new array
function tail(theArray){

let newArr = [];
for(let i=1; i<theArray.length; i++){
newArr.push(theArray[i]);
}
console.log(newArr);
return newArr;
}

tail(array1); //output: [3,5,8,4]

//implement the most basic sorting algorithm there is
//assume the array will always have numbers
//use a while loop to constantly loop theArray until it is sorted
Expand All @@ -76,9 +141,26 @@ function tail(theArray){
//after each for loop check the variable, if true, continue the while loop
//if false return theArray
function sort(theArray){

let swapped = false;
for (let i=0; i<theArray.length; i++){
if (theArray[i] > theArray[i+1]){
let temp = theArray[i];
theArray[i] = theArray[i+1];
theArray[i+1] = temp;
swapped = true;
}
}
if(swapped){
sort(theArray);
}
else{
console.log(theArray);
return theArray;
}
}

sort(array1); //output: [3,4,5,8,11]

exports.map = map;
exports.filter = filter;
exports.find = find;
Expand Down
6 changes: 6 additions & 0 deletions packageTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let dash = require("aca-dash-selena-solis");

let array1 = [11,3,5,8,4];

let headOfArr = dash.head(array1);
console.log(headOfArr);