This javascript should run the function func len times and then return whether it was successful all the times or not. Instead, the for loop is cancelled as soon as func returns false. How can this be explained? (jsfiddle)
function do_something(func, len) {
var res = true;
for (var i = 0; i < len; i++) {
res = res && func(i);
}
return res == true;
}
do_something(function(x) {
console.log(x);
return false;
}, 5);
do_something(function(x) {
console.log(x);
return true;
}, 5);
I would expect 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, but the output looks like this:
0
0
1
2
3
4
resinstead of returningres == true