JSFiddle - React, Tailwind, and code Playground
by tobyworth
JavaScript
function forEach(arr, action){
for(let i=0; i<arr.length; i++){
action(arr[i]);
}
}
function reduce(func, base, arr){
forEach(arr, function(elm){
base += func(elm) ? 1 : 0;
});
return base;
}
let isDoubleMultiple = (x, y) => {
return function(element){
if(element===0) return false;
return element % x === 0 && element % y === 0;
}
}
let isTripleMultiple = (x, y, z) => {
return function(element){
if(element===0) return false;
return element % x === 0 && element % y === 0 && element % z === 0;
}
}
function count(func, arr){
return reduce(func, 0, arr);
}
function countZeroes(arr){
return count(isZero(), arr);
}
alert(count(isDoubleMultiple(2,4), [0,1,2,3,0,4])); // 1
alert(count(isDoubleMultiple(3,9), [1,2,0,3,4,9,27])); // 2
alert(count(isTripleMultiple(3,9,27), [1,2,3,0,4,9,27,54,81,99,101,108])); // 3