PDA

View Full Version : If/Else/If/Else statement not working



Lightro
Mar 20th, 2007, 10:13 PM
Hi.

It seems that when I call this function it doesnt work.. Can anyone tell me why..?



<script type="text/javascript">
<!--
function disp_elif()
{
var number=prompt("Pick a number from 1 to 10")
if (number > 0 && number < 6)
{
alert("I predict that that number is betwenn 1 and 5")
}
else if (number > 5 && number < 10)
{
alert("I predict that number is between 5 and 10")
}
else
{
alert(I predict that number is 10)
}
}
//-->
</script>


thanks!

Philip M
Mar 20th, 2007, 10:53 PM
alert ("I predict that number is 10")

alert("I predict that number is between 6 and 9")

var number=prompt("Pick a number from 1 to 10","")

What if the user enters 34 (or whatever)? That will be taken as 10.

Lightro
Mar 21st, 2007, 02:15 AM
Ok thanks it was thoughs quotation marks that I had forgotten :P

Yea the idea of the script wasnt really for it to make sence but it was just a random script to help me learn Javascript.

It seems to work when I go:


prompt("blablabla")

without me having to go:


prompt("blablabla", "")

glenngv
Mar 21st, 2007, 05:16 AM
The second parameter of the prompt method is the default value. If you want to put a default value to the prompt, specify the second parameter. If you don't specify it, IE displays undefined as the default value while Firefox displays an empty value. So it's better to put an empty parameter to make it the same for all browsers.