Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts

    JavaScript Switch Statement Help

    Could someone help an aging JavaScript noob and tell me why the script below won't display please.

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
                    <title>Fishy Food Chippy</title>
                    <meta charset="utf-8" />
    
    		<script>
      			function fishnChips(){
     			var fish = prompt("What fish do you like with your chips?", "cod");
     			switch (fish)
     			{
    			 case "cod" :
    				alert(cod is + " £2.50 per portion");
                                    break;
     			case "hake" :
    				alert(hake is + " £2.50 per portion");
                                    break;
    			 case "crab" :
    				alert(crab is + " £2.25 per portion");
                                    break;
    			case "squid" :
    				alert(squid is + " £2.20 per portion");
                                    break;
    			case "seagull" :
    				 alert(seagull is + " £2.20 per portion");
                                    break;                               
    			case "chips" :
    				alert(chips are + " £1.25 per portion");
                                    break;                
                            default : 
    				alert("We don't serve goldfish or mermaids in our chipshop");
    				break;
    		        }
                    }  	    					
    		</script>
    	</head>
    	
    <body>
    	
    <p>Fishy Food Chippy</p>
    
    <button onClick="fishnChips()">Try it</button>
    
    </body>
    </html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Change:
    Code:
    alert(cod is + " £2.50 per portion");
    to:
    Code:
    alert("cod is £2.50 per portion");
    Do the same for the other cases.

    It would help you if you check the Javascript console (F12 > Console) to check for errors.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Alternative solution without using switch statement.
    Code:
    function fishnChips(){
    	var fish = prompt("What fish do you like with your chips?", "cod");
    	var prices = {
    		"cod" : 2.5,
    		"hake": 2.5,
    		"crab": 2.25,
    		"squid": 2.2,
    		"seagull": 2.2,
    		"chips": 1.25
    	};
    
    	if (prices[fish]) {
    		alert(fish + " is £" + prices[fish].toFixed(2) + " per portion");
    	}
    	else {
    		alert("We don't serve goldfish or mermaids in our chipshop");
    	}
    }

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Going to give up wearing glasses and start using googles lol. Big thanks glenngv for the speedy reply and also for the alternative solution.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,027
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Be aware that prompts and alerts are regarded as obsolete and should be used only for testing purposes. Use DOM methods to display a message to the user.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •