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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:01:34  来源:igfitidea点击:

Javascript: for-loop not working

javascriptloopsfor-loopclosuresanonymous

提问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 iand jare undefinedand so the loops won't go anywhere.

目前我会假设i并且jundefined,所以循环不会去任何地方。

回答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=0initialisation. You only declared the variables, and undefinedalways yields falsefrom 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>"