diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..d06992418 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -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))); + 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; +}; +const filtArray = [1,2,3,4]; +filter(filtArray, (x) => { + return x > 2; +}); function some(arr, callback) { // Your code here }