I can't figure out why this code is not working out. What am I doing wrong? I need to use document.write
to create my table in Javascript. Any help would be appreciated!
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var rows;
var cols;
function table(rows, cols){
document.write('<table border="1">');
for (i=0, i < rows, i++){
document.write('<tr>');
for (j=0, j < cols, j++) {
document.write('<td>' + 'cell' + '</td>');
}
document.write('</tr>');
}
document.write('</table>');
}
document.write (table(2, 4));
</script>
</body>
</html>
Ok, by now you got an ear full of
document.write
, refer to this about the subject.At a glance your syntax is good with two glaring exceptions is that in a
for
loop, we use semicolons because afor
loop is like a shorthand form of 3 statements and what we have after a statement so browsers know you are finished stating a statement is a;
.The other glaring error is how you are calling the function at the end, it should be:
The following Snippet demonstrates an alternate way of creating tables. Details commented in Snippet.
SNIPPET