469,626 Members | 872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Arrays

Below is a JavaScript program. How can I create an array and modify the code so that the table goes to 100 instead of 10?

I appreciate all your help

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>Table of Calculations</title>
<script type = "text/javascript">
<!--
document.write(
"<table width = '50%' border = '1'><tr> " +
"<th>number</th> <th>square</th> <th>cube</th>" );
document.write(
"<tr><th>" + 0 + "</th>" + "<th>" + 0 * 0 +
"</th>" + "<th>" + 0 * 0 * 0 + "</th></tr>" );
document.write(
"<tr><th>" + 1 + "</th>" + "<th>" + 1 * 1 +
"</th>" + "<th>" + 1 * 1 * 1 + "</th></tr>" );
document.write(
"<tr><th>" + 2 + "</th>" + "<th>" + 2 * 2 +
"</th>" + "<th>" + 2 * 2 * 2 + "</th></tr>" );
document.write(
"<tr><th>" + 3 + "</th>" + "<th>" + 3 * 3 +
"</th>" + "<th>" + 3 * 3 * 3 + "</th></tr>" );
document.write(
"<tr><th>" + 4 + "</th>" + "<th>" + 4 * 4 +
"</th>" + "<th>" + 4 * 4 * 4 + "</th></tr>" );
document.write(
"<tr><th>" + 5 + "</th>" + "<th>" + 5 * 5 +
"</th>" + "<th>" + 5 * 5 * 5 + "</th></tr>" );
document.write(
"<tr><th>" + 6 + "</th>" + "<th>" + 6 * 6 +
"</th>" + "<th>" + 6 * 6 * 6 + "</th></tr>" );
document.write( "<tr><th>" + 7 + "</th>" + "<th>" +
7 * 7 + "</th>" + "<th>" + 7 * 7 * 7 +
"</th></tr>" );
document.write(
"<tr><th>" + 8 + "</th>" + "<th>" + 8 * 8 +
"</th>" + "<th>" + 8 * 8 * 8 + "</th></tr>" );
document.write(
"<tr><th>" + 9 + "</th>" + "<th>" + 9 * 9 +
"</th>" + "<th>" + 9 * 9 * 9 + "</th></tr>" );
document.write( "<tr><th>" + 10 + "</th>" + "<th>" +
10 * 10 + "</th>" + "<th>" + 10 * 10 * 10 +
"</th></tr>" );
document.write( "</table>" );
// -->
</script></head><body></body></html>
Nov 26 '06 #1
3 822
AricC
1,892 Expert 1GB
There is a much prettier way to do this but you need to use a loop, Change index <= 100 to whatever number you want to see the square and cube of:

[html]<html>
<head><title>Table of Calculations</title>
<script type="text/javascript">
<!--
var index = 0;

document.write("<table width='50%' border='1'>");
document.write("<tr><td>Number</td><td>Square</td><td>Cube</td></tr>");

for( index=0;index <= 100; index += 1 )
{
document.write("<tr><td>" + index + "</td>" + "<td>" + index * index + "</td>" + "<td>" + index * index * index + "</td></tr>");
index += 1;
}

// -->
</script></head><body></body></html>[/html]
Nov 27 '06 #2
There is a much prettier way to do this but you need to use a loop, Change index <= 100 to whatever number you want to see the square and cube of:

[html]<html>
<head><title>Table of Calculations</title>
<script type="text/javascript">
<!--
var index = 0;

document.write("<table width='50%' border='1'>");
document.write("<tr><td>Number</td><td>Square</td><td>Cube</td></tr>");

for( index=0;index <= 100; index += 1 )
{
document.write("<tr><td>" + index + "</td>" + "<td>" + index * index + "</td>" + "<td>" + index * index * index + "</td></tr>");
index += 1;
}

// -->
</script></head><body></body></html>[/html]


Thank you so much for your help. I really appreciate it.
Nov 28 '06 #3
AricC
1,892 Expert 1GB
No problem kind of an interesting little script.
Nov 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by Matteo Settenvini | last post: by
5 posts views Thread by JezB | last post: by
3 posts views Thread by Michel Rouzic | last post: by
1 post views Thread by Rob Griffiths | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
6 posts views Thread by Robert Bravery | last post: by
16 posts views Thread by mike3 | last post: by
weaknessforcats
29 posts views Thread by weaknessforcats | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.