3

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>"
1
  • 1
    For reference, you can simply do change_text += "new text"; instead of change_text = change_text + "new text"; Commented Feb 19, 2013 at 15:00

7 Answers 7

8

You need to initialize your iterators:

for(var i = 0; i < num; i++)
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

2

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.

Comments

2

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

Comments

2

You need to initialize i and j, like this:

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

Comments

2

Not initialized i,make i=0

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.