I'm working through Eloquent Javascript and there's an exercise to make an every function, which takes an array and a function and returns true or false depending on what all the items in the array return after going through the function.
I'm confused because when I do console.log() within the function, I get the boolean twice...but when I do console.log(every(arr, func)), I get undefined.
var every = function(arr, req){
arr.map(function(item){
return req(item);
}).reduce(
function(total, num){
// this returns: true
// true
console.log(total && num);
return total && num;
});
}
// This returns undefined
console.log(every([NaN, NaN, NaN], isNaN));
So why do I get true twice inside my function, and why do I get undefined?
I'm using node as a console.
every, soconsole.log(every(...))will always be undefined.returnaside, you really should just usearr.every(req), and if you go forreducenonetheless you should always pass an initial accumulator value to cover empty arrays.