0

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 = 0 and 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!

1
  • 1
    its simply because when i reaches 2048 the outer loop cannot get executed again Commented Jun 26, 2015 at 13:35

6 Answers 6

2

Just initialize i every time before inner iteration:

var a = 0, b = 2048;
var i, l = 2048;

for(; a < b; a++) {
    for(i = 0; i < l; i++) {
        console.log(a, b, i, l);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You didn't reload i to 0 between a loops. Here the fix :

var a = 0, b = 2048;
var i = 0, l = 2048;

for(a = 0; a < b; a++) {
    for(i = 0; i < l; i++) {
        console.log(a, b, i, l);
    }
}

Comments

2

The reason this is happening is because you never reset i, so the inner loop will only happen during the first iteration on the outer loop. Thereafter i will always be greater than l.

Try 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);
    }
    i = 0;
}

Comments

1

the loop goes like this:

for a = 0; itarates while i becomes 2048. then for a =1 the i is 2048, thus i not < l and thus not getting into the loop again.

var a = 0, b = 2048;

for(; a < b; a++) {
    var i = 0, l = 2048;
    for(; i < l; i++) {
        console.log(a, b, i, l);
    }
}

I believe this is what you want.

Comments

1

This is a scope problem. Variables a and i are not defined in the scope of your for loops. Define them inside this way:

var b = 2048,
    l = 2048;

for(var a = 0; a < b; a = a + 1) {
    for(var i = 0; i < l; i = i + 1) {
        console.log(a, b, i, l);
    }
}

edit: squint is absolutely correct, JavaScript doesn't use block scope, for which I mistook as the issue. Although, you should always consider defining the variables that increment inside the declaration of your for loops to eliminate confusion/ambiguity.

1 Comment

It's not a scope problem. Your code doesn't change the variable scope at all. The only problem was with the failure to reset i in the loop, which your answer does fix.
0

Try one loop

var a = 0, b = 2048;
var i = 0, l = 2048;

for(; a <= b&&i<=l; a++,i++) {
    console.log(a, b, i, l);
}

With two loops it i is never reset and so only get iterated once, hence the result of 0, 2048, 0..2047, 2048

1 Comment

this won't produce the expected output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.