The purpose of my program is to create two functions, some()
and every()
, which emulate the functions Array.prototype.some()
and Array.prototype.every()
, respectively.
My code:
function every(array, callback) {
array.forEach(function(element) {
if (!callback(element)) {
return false;
}
});
return true;
}
function everyB(array, callback) {
for (var i = 0; i < array.length; i++) {
if (!callback(array[i])) return false;
}
return true;
}
function everyC(array, callback) {
for (element of array) {
if (!callback(element)) return false;
}
return true;
}
These are the 3 versions I made when checking that after executing:
console.log(every([NaN, NaN, 4], isNaN));
the result is true
, while:
console.log(everyB([NaN, NaN, 4], isNaN));
and
console.log(everyC([NaN, NaN, 4], isNaN));
return false
, which is correct.
The only thing that varies between these 3 functions are the iteration types used
to go through the array, and I can not find the reason why every()
does not work as expected.
Documentation: