This is weird, I'm having issues with simple nested for loops in javascript.
My code is like this:
var a = 0, b = 2048;
var i = 0, l = 2048;
for(; a < b; a++) {
for(; i < l; i++) {
console.log(a, b, i, l);
}
}
So while, I'm expecting an output like this (0..2047), 2048, (0..2047), 2048, I'm having this output: 0, 2048, 0..2047, 2048 where the first variable: a doesn't simply iterate from 0 to 2047.
Rephrasing the concept: while the inner loop iterates correctly, the outer one is executed only once at index 0.
I'm sure it's a simple and silly issue, but I can't really spot that..
COMMENT
Thank you all for finding this issue, it's incredible how I couldn't see that. I'm accepting simon's answer because it seems cleaner and more elegant to me:
- He doesn't reinitialize variable as in
for(var i = 0;...)but just reset it - He includes the variable reset in the for statement rather than after every iteration
- He doesn't declare variables
var a = 0, i = 0and then reset that in every for statement - He uses the regular increment
- He declares every variable at the beginning of the snippet instead of declaring them at different times in the execution
Thanks again!