I'm attempting to create a function which accepts an array and a callback function. The function should return true if all values in the array passed to the callback return true, otherwise, return false. But I'm not sure what I'm doing incorrectly
const every = function(arr, callback) {
arr.forEach(function(element) {
if(!callback(element)) {
return false
}
})
return true
};
every([1, 2, 3, 4, 5], function(val) {
return val < 2
});
expected results => false but I'm getting true.
{}at the end of it looks strangeArray.prototypehas aneverythat does what you want, e.g.[1, 2, 3, 4, 5].every(val => val < 2)Array.prototype.every