Given the code
var loops = 5;
var counter = 0;
function foo(counts){
var baz = 0;
for(var i=0;i<loops;i++){
baz += i + counts;
}
return baz;
}
for(var i=0;i<foo(counter);i++){ //issue in this line
console.log(i);
}
Every time that the for loop increments, it calls the foo function again.
Why is it doing this and is there a way to prevent this?
I understand that I can just store the return in a variable, but I'm still rather curious why this happens.
It doesn't happen in other languages like python.
The way I tested that it does this is by setting a breakpoint in the foo function.