Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Simple javascript question

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>    

Name: Anonymous 2007-09-24 0:41 ID:uEJGP2jE

>>1
Avoid functions?

Get the fuck out of /prog/, you are not welcome here. You need at least EXPERT PROGRAMMER status to even be considered.

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-09-24 1:10 ID:KjGj4R3Y

>>7
Try putting <html><body> your code </body></html>.

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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
      
        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>
</body></html>

Name: Anonymous 2007-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: Anonymous 2007-09-24 1:57 ID:2RRsQW5i

ok i got it working but only when I took out some of DW's default crap.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">;

That is what DW puts at the top of every new sheet automatically, but i removed "xmlns=""" target="_blank">http://www.w3.org/1999/xhtml">"; that and it worked fine. Weird.

Thanks for all your help guys!

Name: Anonymous 2007-09-24 6:34 ID:LInLkxiL

DOCTYPE is ENTERPRISE

Use HTML 5.

Name: Anonymous 2007-09-24 6:56 ID:d8PryZGs

document.write = fail
putting html comments inside script tags = fail

Name: Anonymous 2007-09-24 8:39 ID:2RRsQW5i

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: Anonymous 2007-09-24 9:18 ID:LInLkxiL

Fuck the browsers who don't support that, let's give these users a reason to upgrade.

Name: Anonymous 2007-09-24 9:29 ID:380wO8vC

use the DOM

Name: Anonymous 2007-09-24 10:28 ID:LInLkxiL

use the force

Name: Anonymous 2007-09-24 10:41 ID:Heaven

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

SINCERELY,
EXPERT PROGRAMMER

Name: Anonymous 2007-09-24 10:58 ID:F8n9rVbp

stfu, fag

Name: Anonymous 2007-09-24 11:01 ID:2R7RsT7e

>>20
OKAY YOU FUQIN ANGERED AN EXPERT DOM KNIGHT

Name: Anonymous 2007-09-24 11:52 ID:d8PryZGs

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.

Name: Anonymous 2007-09-24 12:58 ID:8Nxi2eXG

Isn't prompt for strings?

Name: Anonymous 2007-09-24 13:03 ID:8Nxi2eXG

I think someone forgot to parse to integer

Name: Anonymous 2007-09-24 15:34 ID:KjGj4R3Y

>>23
Most are.

Name: Anonymous 2007-09-24 15:34 ID:KjGj4R3Y

>>25
I think someone didn't need to since the multiplication makes the type explicit.

Name: Anonymous 2009-08-16 22:35

Lain.

Name: Anonymous 2009-12-24 22:05

aaa > aaa

Name: Anonymous 2009-12-24 22:05

> aaa

Name: Anonymous 2009-12-24 22:05

aaa

Name: Anonymous 2010-12-09 6:19

Name: Anonymous 2010-12-17 1:37

Erika once told me that Xarn is a bad boyfriend

Name: Anonymous 2012-06-25 22:41

肄唂䄁㡧┕”琧颀祶ቢ䒂阑⎁㉳坥䀸占艑荥煨薗उ葑ॵᔘ蔵獣敀✧㑶禆喀杂䈱べ䈂荩㜀䥰ဈ鞅镕剩瘥ᡑ葆襰匕肕晢㎄㥰ͱ璐╓Ɩň虖塨ᔀၱ餂匕顳㝸瀲塩㔵፵墖⅀嘔ȥ莕ᔨ▅䔢⌤⢘ኈ嘒ᠩ㥵匉ᅁ晠䞁⅔斃⚉䢔ᘉ䕔愥鐒薇┐限Ζ熙葉բ钕Ն猅饑䥥ܳᅠ䝵蕱䞘〨餔╗肓Ԡ̙剷㍣䤀睵䝶耰脘襹楠頦桓眶垂㊖㘢ተ荔偖甶褦摓᠄鐃㡀ᕗ䔇⠄䑑卙㍰蝕楢ᥐ喓焇⡴☀㤩㔡䌴䍳⑷獃妕朦艆䉘霰䂘艷❤Ђࠅݒ夣㝥挷㒀ij褩ݠ頓䝧䡐ၗ㐗愔要鐇捐䜧㐂塴ᙙ顸慦匒䔄搘錖Ƞ〹⡲え阢昐㌠ेƙ㌀栄㈙朒ᤕဠᐉ᐀䅢斈妁ሁ衂牷ܡ琗袒⌐⍴ㄠ㘧ታ☠銖㠕╹㥩椴ᄴ卧➈ƅ梆怣蚒茷ᚅ̃饒瘑螙䠀怓ፀ煶ゐ鑧桴鄵硕䄸蠗鄦䁃♨阥靁鐈ᙘ⍹じ␱褲衁甄ࡗᑧᘨ犂挑䅦ጇᘤ鑹䂗䅤ց搱鄵䝉耢扩遑朕皐萘գ䥧隈⠗厕㑳䐅

Don't change these.
Name: Email:
Entire Thread Thread List