PDA

View Full Version : switch{class 1:} problem


andrew1234
11-13-2002, 09:02 AM
Hi
can anyone please tell me why the second script does not work

script 1 shows
that variable fred =1 so the alert in case 1: shows

but in the second
script i used a fred=prompt("hello",1)
but this one goes and displays the default alert("nothing")
why won't this script show its class 1:

thanks

andrew

---------------------------------------------script 1
<script language="javascript">


var fred=1

switch (fred)
{
case 1:
alert("rad");
break;


case 6:
alert("cool");
break;


case 9:
alert("radical");
break;

//default:
//alert("nothing");
//break;

}
</script>

--------------------------------------------------script 2--
<script language="javascript">
var fred

var fred=prompt("hello",1);

switch (fred)
{
case 1:
alert("yeah bady");
break;


case 6:
alert("cool");
break;


case 9:
alert("radical");
break;

default:
alert("nothing");

}
</script>

glenngv
11-13-2002, 09:12 AM
the return value of the prompt() method is a string.
so you need to surround each case in quotes.

var fred=prompt("hello",1);

switch (fred)
{
case "1":
alert("yeah bady");
break;
...

andrew1234
11-13-2002, 01:17 PM
Thanks alot

that was silly of me


thanks again

Andrew