Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts

    displaying results from a function

    i'm just trying out some stuff i learned from this JS book, and i made a script (or tried to) that should prompt for a number 1-26, and it will return (with document.write) the corresponding letter of the alphabet. when i try it out, i dont get any error messages, but when it tries to display the letter, it shows the coding for the function. here's the script (dont yell at me if it's so obvious i'm new hehe)...

    Code:
    <html>
    <head>
    <title>Learning JS</title>
    <script language="javascript">
    function ber(anum) {
    anumdisp = anum--;
    disp = alpha.charAt(anumdisp);
    alpha = "abcdefghijklmnopqrstuvwxyz";
    }
    </script>
    </head>
    <body>
    <script language="javascript">
    anum = prompt("Type in a number 1 - 26 and get the corresponding letter of the alphabet.");
    document.write(ber + " is number " + anum + " " + "in the alphabet.");
    </script>
    </body>
    </html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    function ber(anum) {
    anumdisp = anum--;
    alpha = "abcdefghijklmnopqrstuvwxyz";
    disp = alpha.charAt(anumdisp);
    return disp;
    }

    ...

    document.write(ber(anum) + " is number " + anum + " "
    + "in the alphabet.");
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    <title>Learning JS</title>
    <script language="javascript">
    function ber(anum) {
    anumdisp = anum--;
    alpha = "abcdefghijklmnopqrstuvwxyz";
    disp = alpha.charAt(anumdisp);
    return disp;
    }
    }
    </script>
    </head>
    <body>
    <script language="javascript">
    document.write(ber(anum) + " is number " + anum + " " + "in the alphabet.");
    </script>
    </body>
    </html>
    so i guess that is the revised edition. but when i try it i get two error. 1) line 11 - syntax error, 2) line 16 - anum is undefined

    ???

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Three things:

    You have a bonus bracket at the end of your function }

    The prompt() got removed in your second <script> block.

    I changed anum-- to --anum. That way it decrements before assigning. Then you get a=1, b=2, c=3.. instead of a=0, b=1, c=2....

    <html>
    <head>
    <title>Learning JS</title>
    <script language="javascript">
    function ber(anum) {
    anumdisp = --anum;
    alpha = "abcdefghijklmnopqrstuvwxyz";
    disp = alpha.charAt(anumdisp);
    return disp;
    }
    </script>
    </head>
    <body>
    <script language="javascript">
    anum = prompt("Type in a number 1 - 26 and get the corresponding letter of the alphabet.");
    document.write(ber(anum) + " is number " + anum + " in the alphabet.");
    </script>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    thanks, that one works. so what needed to be done was change the document.write thing to display ber(anum) instead of plain old ber (causing it to show the coding for the function, i guess). and return disp; completes it somehow, and anum-- changed to --anum. i'm learning

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    so here's a followup.... if i wanted to make it an input textbox instead of a prompt, how would this work? i'd like it so as soon as you type 1, it writes "a" next to "LETTER" and if you then type a 2 after the 1, it changes to "L" instead (lower case, i just capitalized it so it's easier to see). so basically, i want it to change automatically when the value of the textbox changes.

    Code:
    <html>
    <head>
    <title>Learning JS</title>
    <script language="javascript">
    function ber(anum) {
    anumdisp = --anum;
    alpha = "abcdefghijklmnopqrstuvwxyz";
    disp = alpha.charAt(anumdisp);
    return disp;
    }
    </script>
    </head>
    <body>
    Type a number 1-26 to get its corresponding letter of the alphabet.
    <br>
    <b>NUMBER</b>
    <input type=text size=2 name=anum><br><b>LETTER</b>
    <script language="javascript">
    document.write(ber(anum) + " is number " + anum + " in the alphabet.");
    </script>
    </body>
    </html>

  • #7
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    This is a bit more complicated. You call the ber() function everytime the user presses a key with the "onkeyup" event. Then it sets the content (innerHTML) of a div equal to the value of the text box.

    <html>
    <head>
    <title>Learning JS</title>
    <script language="javascript">
    function ber(fieldname, anum) {
    anumdisp = --anum;
    alpha = "abcdefghijklmnopqrstuvwxyz";
    disp = alpha.charAt(anumdisp);
    document.getElementById("mydiv").innerHTML=disp;
    }
    </script>
    </head>
    <body>
    Type a number 1-26 to get its corresponding letter of the alphabet.
    <br>
    <b>NUMBER</b>
    <form>
    <input type="text" name="myfield" onkeyup="ber(this, this.value);" size="2" maxlength="2">
    </form>
    <br>
    <div id="mydiv">
    </div>
    </body>
    </html>

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i good tip when using forms where data is enterd is to have a button to start your script when it is clicked.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •