www.webdeveloper.com
Results 1 to 2 of 2

Thread: switch statment using string

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    81

    switch statment using string

    I'm trying to do something like this
    Code:
    if ((grade == "BPlus") && (mark == "70")) {alert("Pretty good<br />");}
    but since I have alot of if statements, I figure I should put it in a switch statement.
    When I tried to do that, it doesn't work. Not sure why.
    It needs to be in a string (i.e. grade and mark).

    Here's my code:

    <html>
    <body>
    Code:
    <script type="text/javascript">
    
    function test(){
    	var grade="BPlus";
    	var mark="90";
    
    	switch (grade){
    		case "APlus": 
    			if (mark == "90"){document.write("Good job<br />");}
    		break;
    		case "BPlus": 
    			if (mark == "70"){document.write("Pretty good<br />");}
    		break;
    		case "FMinus": 
    			if (mark == "35"){document.write("Thanks for trying!!<br />");}
    		break;	
    		default:  document.write("Unknown grade<br />")
    	}
    }
    </script>
    
    
    </body onload="test()">
    </html>

  2. #2
    Join Date
    Oct 2013
    Posts
    602
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>grade</title>
    </head>
    <body onload="test()">
    <script>
    
    function test(){
    	var grade="BPlus";
    	var mark="70";
    
    	switch (grade){
    		case "APlus": 
    			if (mark == "90"){document.getElementById('remark').innerHTML = "Good job<br />";}
    		break;
    		case "BPlus": 
    			if (mark == "70"){document.getElementById('remark').innerHTML = "Pretty good<br />";}
    		break;
    		case "FMinus": 
    			if (mark == "35"){document.getElementById('remark').innerHTML = "Thanks for trying!!<br />";}
    		break;	
    		default:  document.getElementById('remark').innerHTML = "Unknown grade<br />";
    		break;
    	}
    }
    </script>
    
    <div id="remark"></div>
    
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles