Name: Anonymous 2007-09-24 0:37 ID:2RRsQW5i
I'm sure this is pretty basic stuff for the people on this board, but I'm just learning javascript so I'm struggling the like the said little lurker I am.
I have an assignment due tomorrow that requires me to write a page that requests the first and last numbers of a sequence from the user and then takes those two numbers and every number in between and displays a table with the numbers and their squares and cubes.
I really thought the script I had written would get it done but after the user enters the second number nothing happens and it just ignores the loop I wrote in. If anyone could tell me what is wrong with my script (posted below) or a different way to do it I would be grateful. I would like to avoid using functions for now as I am still a newb and am not particularly fond of them though I know I will be needing to use them alot at some point. Any help would be great anon!
<script language="Javascript" type="text/javascript">
<!-- Hide script from old browsers
document.write('<center><table width="50%" border="1">');
document.write('<tr align="center"><td>' + "Numbers" + '<\/td><td>' + "Square" + '<\/td><td>' + "Cube" + '<\/td><\/tr>');
ans = prompt("Enter the first number of the sequence.","")
try
{
if (!ans || isNaN(ans))
{
throw new Error('That is not a number.')
}
ans2 = prompt("Enter the last number in the sequence.","")
if (!ans2 || isNaN(ans2))
{
throw new Error('That is not a number. Hit F5 and re-enter')
}
}
catch(errMsg)
{
alert(errMsg.message)
}
finally
{
for(i = ans; i > ans2; i++)
{
document.write('<tr align="center"><td>' + i + '<\/td><td>' + i*i + '<\/td><td>' + i*i*i + '<\/td><\/tr>');
}
}
// End hiding script from old browsers -->
</script>
I have an assignment due tomorrow that requires me to write a page that requests the first and last numbers of a sequence from the user and then takes those two numbers and every number in between and displays a table with the numbers and their squares and cubes.
I really thought the script I had written would get it done but after the user enters the second number nothing happens and it just ignores the loop I wrote in. If anyone could tell me what is wrong with my script (posted below) or a different way to do it I would be grateful. I would like to avoid using functions for now as I am still a newb and am not particularly fond of them though I know I will be needing to use them alot at some point. Any help would be great anon!
<script language="Javascript" type="text/javascript">
<!-- Hide script from old browsers
document.write('<center><table width="50%" border="1">');
document.write('<tr align="center"><td>' + "Numbers" + '<\/td><td>' + "Square" + '<\/td><td>' + "Cube" + '<\/td><\/tr>');
ans = prompt("Enter the first number of the sequence.","")
try
{
if (!ans || isNaN(ans))
{
throw new Error('That is not a number.')
}
ans2 = prompt("Enter the last number in the sequence.","")
if (!ans2 || isNaN(ans2))
{
throw new Error('That is not a number. Hit F5 and re-enter')
}
}
catch(errMsg)
{
alert(errMsg.message)
}
finally
{
for(i = ans; i > ans2; i++)
{
document.write('<tr align="center"><td>' + i + '<\/td><td>' + i*i + '<\/td><td>' + i*i*i + '<\/td><\/tr>');
}
}
// End hiding script from old browsers -->
</script>