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
|
7 Answers
Reset to default 8You 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>"
change_text += "new text";
instead ofchange_text = change_text + "new text";
– James Donnelly Commented Feb 19, 2013 at 15:00