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
55 changes: 47 additions & 8 deletions 06week/higherOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@

const assert = require('assert');

function forEach(arr, callback) {
// Your code here
}

function map(arr, callback) {
// Your code here
}
// function forEach(arr, callback){
// ** method
// var arr1 = [1, 2, 3,]
// arr1.forEach(function(entry){
// console.log(entry)
// })

function filter(arr, callback) {
// Your code here
// this is forEach function that iterates over each item in array with defined function. for each item
// in array dot length perform inputfunction
const forEach = (inputArray, inputFunction) => {
for (let i = 0; i < inputArray.length; i++){
inputFunction(inputArray[i]);

}
};
//this is the array
const fullArray = [1, 2, 3];
// this is a function that uses fullArray as the argument and then console logs it as the function.
function funky(fullArray) {
console.log(fullArray);
}
//calling fullArray and funky to run the forEach function logs the exact same thing as a forEach method
forEach(fullArray, funky);

// function map(arr, callback) {
//
// }
const map = (arg1, arg2) => {
let nums = [];
arg1.forEach(index => nums.push(arg2(index)));
Copy link

Choose a reason for hiding this comment

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

This map function works perfectly, but some of the things inside aren't named well. index isn't the index, it's the item, and the return array won't always be full of numbers, so nums doesn't make sense. But it works!

return nums;
};
const add2 = i => i+2;
const array = [1, 2, 3, 4];
map(array, add2);
// function filter(arr, callback) {
// // Your code here
// }
const filter = (inpt , inFun) => {
let arr1 = [];
for (let i = 0; i < inpt.length; i++){
if(inFun(inpt[i])) {
arr1.push(inpt[i]);
}
}return arr1;
Copy link

Choose a reason for hiding this comment

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

Put the return on its own line.

};
const filtArray = [1,2,3,4];
filter(filtArray, (x) => {
return x > 2;
});
function some(arr, callback) {
// Your code here
}
Expand Down