PDA

View Full Version : problems w/ a table ( written in JS )


SerenadeSP
11-14-2005, 07:36 PM
I am having trouble with a page that I'm trying to create for a class.

Heres the code:

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Odd Sum table</title>

<style type="text/JavaScript">

document.tags.th.color = 'blue';

</style>

<script language="JavaScript">


<!-- a JavaScript program that adds the odd numbers from 1-50 -->
<!--
var sum, x;
x = 1;
sum = 0;

for ( var sum1 = x + sum; x <= 50 ) {

document.writeln( "<center>" + "<caption>" + "Sum of odd number from 1-50" + "</caption>" + "<table border=1 width=50% align="center">" );

document.writeln( "<tr><th>" + "Odd Number" + "</th></tr>" + "<tr><th>" + "sum" + "</th></tr>" + "<tr><td>" + sum1 + "</td></tr>" );

document.writeln( "</table>" + "</center>" );

}


//-->
</script>

</head><body></body>
</html>


I'm trying to make a table that will print the sum of odd integers by itself ( like 1 + 2 = 3, 2 + 3 = 5... etc.)

For some odd reason, the table will not even show! Can someone help?:confused:

nikkiH
11-14-2005, 08:09 PM
Put the script in the body, not the head, if you are writing elements and it isn't a function call that is called from the body.

Though you should really be using DOM methods such as createElement and appendChild...if your teacher isn't teaching such things, and this is a javascript class, I'd take a bit of an exception to that. Document.write is quite old school at this point.