admin管理员组文章数量:1390192
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!
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!
Share Improve this question edited Jun 26, 2015 at 14:20 XCore asked Jun 26, 2015 at 13:17 XCoreXCore 1173 silver badges9 bronze badges 1- 1 its simply because when i reaches 2048 the outer loop cannot get executed again – theodore hogberg Commented Jun 26, 2015 at 13:35
6 Answers
Reset to default 2Just 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);
}
}
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);
}
}
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;
}
the loop goes like this:
for a = 0; itarates while i bees 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.
This is a scope problem. Variables Define them inside this way:a
and i
are not defined in the scope of your for loops.
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.
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
本文标签: nodejsJavascript nested for loop not workingStack Overflow
版权声明:本文标题:node.js - Javascript nested for loop not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744749880a2623111.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论