I built a function every that should iterate over an array and return true if the action (for ex. element < 10) performed on all the elements is true. Here's my code:
function every(array, action) {
var trueOrFalse = true
for (var i = 0; i < array.length; i++)
trueOrFalse = trueOrFalse && action(array[i]);
if (trueOrFalse = true) return true;
else return;
}
array1 = [1,2,3,4,5,6,7,8,9,10,11]
console.log(every(array1, function(element) {
return element < 10
}))
I don't see anything wrong. With array1 it returns true even if it contains numbers > 10. Where's the problem?
Thanks
array.prototype.every()method?actiongives a falsey result,return falseimmediately. Then putreturn trueafter the loop.for (var i = 0; i < array.length; i++) { if (!action(array[i])) return false } return true