...

View Full Version : Having problems getting my table to display



bedwards6968
03-29-2012, 01:58 AM
I'm having to write a program in javascript that appends a html table to show numerical values... Here is my code which I'm stuck at...


<!DOCYTYPE html>
<html>
<head><title>Numeric Bases</title>
<style type="text/css">
body { padding: 2em; margin: 0px auto; background-color: white; color: #222; font-family: "Helvetica", "Arial", sans-serif;}
h1 {border-bottom: 1px solid #aaa; text-align: center; }
p#countdown { font-size: 150px; text-align: center; margin: 0; padding: 0;}
table { width: 50%; margin: 0 auto; }
caption { font-size: xx-large; font-weight: bold; }
thead > tr {background-color: #aaa; }
td, th { text-align: center; padding: 1em; border: 1px solid #ccc; }
tr.alternate { background-color: #eee; }
</style>
<script type="text/javascript" src="num-bases1.js"></script>
</head>


<table>
<caption>Numerical values in various bases</caption>
<thead><tr><th>Dec</th><th>Binary</th><th>Octal</th><th>Hex</th></tr></thead>
<tbody></tbody>
</table>


</body></html>


/*
* The following function should generate table cells
* that contain an integer (in base 10), and its equivalent
* in Binary (base 2), Octal (base 8), and Hexidecimal (base 16).
*/
var num_bases = function(max_number) {

output = '';

for ( var count = 1; count <= max_number; count++){
var binary = count.toString(2);
var octal = count.toString(8);
var hex = count.toString(16);

output += '<tr><td>' + count + '</td><td>' + binary + '</td><td>' + octal + '</td><td>' +
hex + '</td></tr><tr class="alternate"><td colspan="4"></td></tr>';
}
// TODO:
// Write a for loop that appends HTML for the
// table row and cells to the output.
//
// The following is an example of a table row:
//
// <tr> <td>4</td> <td>100</td> <td>4</td> <td>4</td> </tr>
//
// Every other row should have an "alternate" class:
//
// <tr class="alternate">

document.getElementById('num-bases').innerHTML = output;

}
window.onload = function() {
num_bases(50);

}

Old Pedant
03-29-2012, 02:22 AM
document.getElementById('num-bases').innerHTML = output;

You don't HAVE any element on your page with the id "num-bases".

And doing this via innerHTML is a bad thing, anyway.

And you clearly misunderstood the directions about "alternate".

Old Pedant
03-29-2012, 02:23 AM
Oh, and you are missing the <body> tag.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum