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
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')
}
}
Get the fuck out of /prog/, you are not welcome here. You need at least EXPERT PROGRAMMER status to even be considered.
Name:
Anonymous2007-09-24 0:46 ID:2RRsQW5i
right, you are not helpful.
To anyone who is not an arrogant shit and doesn't mind helping people that are less knowledgeable: I am not completely averse to using functions, I'm just not very good with using them efficiently so I don't mind typing the same code over and over, but if this can be done easily with functions by all means help me learn.
Name:
Anonymous2007-09-24 0:48 ID:KjGj4R3Y
>>1 avoid using functions
Functions are fun. You'd better learn to love them now: your programming will become worlds better. After you do that, make your variable names better.
Name:
Anonymous2007-09-24 0:53 ID:KjGj4R3Y
>>1
Anyway, presumably the issue here is that your for loop's termination condition is backwards. You're saying, ``while i is greater than the high end of the range, do this thing''. Obviously, i never is.
Name:
Anonymous2007-09-24 0:56 ID:2RRsQW5i
AHH! What an obvious mistake! Thank you. I think I misread the textbook on for loop setup because I thought that the termination condition defined when it should stop rather than when it shouldn't stop. Much appreciated and I will certainly work on better variable names.
Name:
Anonymous2007-09-24 1:03 ID:2RRsQW5i
OP here
Hmm, I changed the termination condition to <= but the table doesn't even show up at all. Not even the initial document.write at the beginning of the script is doing anything. Any ideas? I exhausted my limited resources of tests to figure out the problem and got nothing.
Name:
Anonymous2007-09-24 1:10 ID:KjGj4R3Y
>>7
Try putting <html><body> your code </body></html>.
Name:
Anonymous2007-09-24 1:15 ID:2RRsQW5i
still no table, nothing happens after the second number is entered. Thanks for the idea though, I was surprised that I stupidly put all of my script in the <head> tag.
Name:
Anonymous2007-09-24 1:30 ID:2RRsQW5i
well thanks to all the people who tried to help, I gotta get to bed so I guess I'll check in the morning.
Name:
Anonymous2007-09-24 1:44 ID:KjGj4R3Y
>>9
It should work. I cut & pasted your code, and changing ``>'' to ``<='' and adding those tags were the only changes I made:
0~%cat t.html
<html><body>
<script language="Javascript" type="text/javascript">
<!-- Hide script from old browsers
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')
}
}
// End hiding script from old browsers -->
</script>
</body></html>
Name:
Anonymous2007-09-24 1:51 ID:2RRsQW5i
hmm, it worked when i put it in notepad and saved it as an html file but if I try to run it from Dreamweaver the table doesn't work. Do you think it is some discrepancy with XHTML Trans? because that is DW's default though I can't imagine any problems
Name:
Anonymous2007-09-24 1:57 ID:2RRsQW5i
ok i got it working but only when I took out some of DW's default crap.
the html comments hide the script from older browsers that don't understand it, it's part of the class and I have to put it in there so tell it to my professor
Name:
Anonymous2007-09-24 9:18 ID:LInLkxiL
Fuck the browsers who don't support that, let's give these users a reason to upgrade.
JESUS CHRIST FAGGOTS
FINALLY BLOCKS ARE FOR CLEANUP
THEY EXECUTE EVEN IF THERE'S AN ERROR, WHICH IS EXACTLY WHAT YOU DON'T WANT FOR THE CODE YOU DUMPED THERE
the html comments hide the script from older browsers that don't understand it,
Anyone using a browser that hasn't been updated in 15 years deserves to have things break.
it's part of the class and I have to put it in there so tell it to my professor
Your professor is full of shit.