Javascript:for循环不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14960176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: for-loop not working
提问by Kevin Lloyd Bernal
I have this code right here.. where the variable numis the dimension of a n by n square table. The objective is to enter a number and create a table with the number as the dimension.
我在这里有这个代码.. 其中变量num是一个 n 方表的维度。目标是输入一个数字并创建一个以该数字为维度的表格。
I got this code but it doesn't go through the 2 layers of for-loops. After the code execution, the string *change_text* just becomes: <table></table>
我得到了这段代码,但它没有通过 2 层 for 循环。代码执行后,字符串 *change_text* 就变成了:<table></table>
change_text = "<table>";
for (var i; i<num; i++) {
change_text = change_text + "<tr>";
for (var j; j<num; j++) {
change_text = change_text + "<td> asdf </td>";
//code for blue cells
}
change_text = change_text + "</tr>";
}
change_text = change_text+ "</table>"
回答by Joe
You need to initialize your iterators:
您需要初始化您的迭代器:
for(var i = 0; i < num; i++)
回答by Lloyd
You need to specify the starting value for your loops:
您需要为循环指定起始值:
change_text = "<table>";
for (var i = 0; i<num; i++) {
change_text = change_text + "<tr>";
for (var j = 0; j<num; j++) {
change_text = change_text + "<td> asdf </td>";
//code for blue cells
}
change_text = change_text + "</tr>";
}
change_text = change_text+ "</table>"
At present I would assume i
and j
are undefined
and so the loops won't go anywhere.
目前我会假设i
并且j
是undefined
,所以循环不会去任何地方。
回答by t0.
you need to initialize i and j... try this:
你需要初始化 i 和 j ... 试试这个:
change_text = "<table>";
for (var i=0; i<num; i++) {
change_text = change_text + "<tr>";
for (var j=0; j<num; j++) {
change_text = change_text + "<td> asdf </td>";
//code for blue cells
}
change_text = change_text + "</tr>";
}
change_text = change_text+ "</table>"
回答by zs2020
You need to initialize i and j, like this:
您需要初始化 i 和 j,如下所示:
for (var i = 0; i<num; i++)
回答by sasi
Not initialized i,make i=0
未初始化 i,使 i=0
for (var i=0; i<num; i++) {
//code
}
回答by Jordan Casey
Ohhh also I noticed that num isn't defined specifically. Wherever you're getting num from make sure to use parseInt if it could have possibly been passed as a string.num = parseInt(num);
哦,我还注意到 num 没有具体定义。无论您从何处获取 num,请确保使用 parseInt 如果它可能作为字符串传递。num = parseInt(num);
回答by Bergi
You forgot the i=0
/j=0
initialisation. You only declared the variables, and undefined
always yields false
from numeric comparisons which breaks the loop immediately. So change your code to
您忘记了i=0
/j=0
初始化。您只声明了变量,并且undefined
总是false
从立即中断循环的数字比较中产生。因此,将您的代码更改为
change_text = "<table>";
for (var i=0; i<num; i++) {
change_text = change_text + "<tr>";
for (var j=0; j<num; j++) {
change_text = change_text + "<td> asdf </td>";
//code for blue cells
}
change_text = change_text + "</tr>";
}
change_text = change_text+ "</table>"