New to js and I'm attempting to solve a really simple problem. I'm not sure why it's not working though. Must be a js quirk I am unfamiliar with. Can someone tell me why x would return back as undefined thus causing my function to return false when it should return true? I'm trying to replicate the 'every' method for arrays and return false if one of the array elements returns false from my callback.
I've attempted to debug with webstorm and I still could not find the solution. Here is my code,
function every_loop(array, test) {
for (let index = 0; array.length - 1; index++) {
let x = test(array[index]);
if (!x) {
console.log(array[index], index);
return false;
}
}
return true;
}
console.log(every_loop([1, 2, 3, 4, 5], n => n >= 1));
My output is false when it should be true. Also, right before it outputs false, it shows undefined as a value for array[index] which leads me to believe that my for loop parameters are incorrect but that isn't the case either. Any help would be appreciated. Thanks
test()code? Also yourforloop test condition is wrong. edit oh wait I see, sorryindexas well. It is getting out of bound.