Created
April 1, 2020 09:41
-
-
Save ronapelbaum/f2e9c2d9f60c6c8df89f608084d7bc26 to your computer and use it in GitHub Desktop.
High Order Functions - Array.prototype.filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* https://jsfiddle.net/ronapelbaum/qk6mcrae/ | |
*/ | |
// HOF get a func and returns a func | |
function createFilterFunc(fn) { | |
return function(arr) { | |
const res = []; | |
for(let i = 0; i < arr.length; i++) { | |
if(!!fn(arr[i])) { | |
res.push(arr[i]); | |
} | |
} | |
return res; | |
} | |
} | |
// ==================== | |
const arr = [1, 3, 5, 4, 8]; | |
const isEven = d => d%2 === 0; | |
const filterEven = createFilterFunc(isEven); | |
console.log('even array.filter', arr.filter(isEven)); | |
console.log('even filterEven', filterEven(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment