最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: for-loop not working - Stack Overflow

programmeradmin2浏览0评论

I have this code right here.. where the variable num is 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.

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>

    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>"

I have this code right here.. where the variable num is 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.

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>

    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>"
Share Improve this question asked Feb 19, 2013 at 14:42 Kevin Lloyd BernalKevin Lloyd Bernal 3633 gold badges9 silver badges24 bronze badges 1
  • 1 For reference, you can simply do change_text += "new text"; instead of change_text = change_text + "new text"; – James Donnelly Commented Feb 19, 2013 at 15:00
Add a comment  | 

7 Answers 7

Reset to default 8

You need to initialize your iterators:

for(var i = 0; i < num; i++)

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);

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.

you need to initialize i and j... try this:

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>"

You need to initialize i and j, like this:

for (var i = 0; i<num; i++)

Not initialized i,make i=0

 for (var i=0; i<num; i++) {
       //code
    }

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

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>"
发布评论

评论列表(0)

  1. 暂无评论