USA Property - Bad Credit Loans - Computers 2007 - Credit Card - Debt Consolidation

PDA

View Full Version : Help With Switch Please!


gobbly2100
Dec 11th 2007, 3:40 pm
Hey, I don't understand how the switch statement works, I think I am doing everything right but am obviously missing something because I just can't get the damn thing to work.

Here is what I have so far:
var state = prompt("What state are you in?", "");

switch(state.toUppercase()) {
case "CA" :
alert("You live in CA");
break;

case "HI" :
alert("You live in HI");
break;

default :
alert("Heck know's where you live");
break;
}

It just alerts me the default text even if I type in one of those states.

Any help is much appreciated.

Thanks!

hogan_h
Dec 11th 2007, 4:11 pm
Replace

switch(state.toUppercase())

with:

switch(state.toUpperCase())


Works for me.

gobbly2100
Dec 11th 2007, 4:49 pm
Oh whoops, needed to make that to upper case but I noticed it don't work in IE but works perfect in FireFox, stupid IE.

Thanks anyway, I just needed to check I had the right idea about switch statements!

Logic Ali
Dec 11th 2007, 9:08 pm
Oh whoops, needed to make that to upper case but I noticed it don't work in IE but works perfect in FireFox, stupid IE.

Thanks anyway, I just needed to check I had the right idea about switch statements!
I.E. runs the corrected code as it should. Your original code would have generated an error in the console.

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:switch