Higher Order functions Example - 401-advanced-javascript-davetrost/alchemy-fsjs-fall-2019 GitHub Wiki
Using Higher Order Functions in order to create code with abstraction
The 'every' method returns true when the given function returns true for every element in the array. In contrast to the 'some' method, where array.some is a version of the || operator that acts on arrays, and array.every is like the && operator.
Here is an implementation of the "every" function written with basic loops. It reads as follows: Go through every element of the array. At each element, run the test function on the element. If the test function fails, return false and stop evaluating new elements. If the test function passes, continue. If all the elements pass the test function, return true.
function every(array, test) {
for(let i = 0; i < array.length; i++) {
if(!test(array[i]) {
return false;
}
}
return true;
}
Here is an implementation of the "every" function written with higher order functions. It reads as follows: if none of the elements in the array fail to pass the test condition, then every element complies.
function every(array, test) {
return !array.some(element => !test(element));
}