Help With Switch Please!

Discussion in 'JavaScript' started by gobbly2100, Dec 11, 2007.

  1. #1
    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;
    		}
    Code (markup):
    It just alerts me the default text even if I type in one of those states.

    Any help is much appreciated.

    Thanks!
     
    gobbly2100, Dec 11, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace
    
    switch(state.toUppercase())
    
    Code (markup):
    with:
    
    switch(state.toUpperCase())
    
    Code (markup):
    Works for me.
     
    hogan_h, Dec 11, 2007 IP
  3. gobbly2100

    gobbly2100 Banned

    Messages:
    906
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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!
     
    gobbly2100, Dec 11, 2007 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    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
     
    Logic Ali, Dec 11, 2007 IP