Javascript: for-loop not working

27,214

Solution 1

You need to initialize your iterators:

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

Solution 2

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

Solution 3

Not initialized i,make i=0

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

Solution 4

You need to initialize i and j, like this:

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

Solution 5

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>"
Share:
27,214
Kevin Lloyd Bernal
Author by

Kevin Lloyd Bernal

Updated on December 10, 2020

Comments

  • Kevin Lloyd Bernal
    Kevin Lloyd Bernal over 3 years

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