Beginning JavaScript - create table -
i have create array using loop produce numbers 0-24. print these numbers in order in 5x5 "table" using loop.
the output should be: 0 1 2 3 4 x 5 6 7 8 9 x 10 11 12 13 14 x... 20 21 22 23 24
i can't figure out how create table.
here code:
// calculate numbers 0-24 var numbers = []; (var 0; < 25; i++) { numbers[i] = i; } // create table (var row = 0; row < 4; row++) { document.write('<tr>'); (var col = 0; col < 4; col++) { document.write('<td>' + numbers + '</td>'); } document.write('</tr>'); }
the problem accessing array without index. try instead:
var numbers = []; ( var 0; i<25; i++) { numbers[i]= i; } //create table var = 0; var table = "<table>"; (var row=0; row<5; row++) //changed 4 5 { table += "<tr>"; (var col=0; col<5; col++) //changed 4 5 { table += "<td>" + numbers[i] + "</td>"; //numbers numbers[i] i++; } table += "</tr>"; } table += "</table>"; document.write(table);
update: taking @jon p answered updated answer write once.
Comments
Post a Comment