...

View Full Version : displaying results from a function



Ricky158
07-17-2003, 08:51 AM
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 :p hehe)...



<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>

glenngv
07-17-2003, 11:08 AM
function ber(anum) {
anumdisp = anum--;
alpha = "abcdefghijklmnopqrstuvwxyz";
disp = alpha.charAt(anumdisp);
return disp;
}

...

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

Ricky158
07-17-2003, 06:13 PM
<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

???

arnyinc
07-17-2003, 06:53 PM
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>

Ricky158
07-17-2003, 09:52 PM
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 :)

Ricky158
07-17-2003, 10:02 PM
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.



<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>

arnyinc
07-17-2003, 10:23 PM
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>

scroots
07-18-2003, 01:09 AM
i good tip when using forms where data is enterd is to have a button to start your script when it is clicked.

scroots



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum