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.
foo(counter)calls foo until baz >= i as you ask it to. How is that a mystery? A for loop executes the test in between the ; ; each timefor(var i=0, n=foo(counter); i<n; i++){ console.log(i);