Skip to content

Instantly share code, notes, and snippets.

@annajmorton
Last active June 2, 2016 14:02
Show Gist options
  • Save annajmorton/38a8898e7f14556ae0458f900ea09943 to your computer and use it in GitHub Desktop.
Save annajmorton/38a8898e7f14556ae0458f900ea09943 to your computer and use it in GitHub Desktop.
Exercise Solutions - Switch

Switch Statement

  1. Create an HTML file named switch_js.html within the ~/vagrant-lamp/sites/codeup.dev/public folder on your Mac.

  2. Add a <script> tag to your HTML to link up switch.js to your file.

    <!DOCTYPE html>
    
    <html>
    
    	<head>
    	
    		<title>JavaScript Statements Switch</title>
    		
    		<meta charset="utf8">
    
    	</head>
    
    
    
    	<body>
    
    		<script src="switch.js" ></script>
    	</body>
    	
    </html>	
    'use strict';
    //see solutions below
  3. The following line generates a random number between 0 and 5.

var luckyNumber = Math.floor(Math.random()* 6)

Now, suppose there's a promotion in Walmart, If your lucky number is 0 you have no discount, if your lucky number is 1 you'll get a 10% discount, if it's 2, discount is 25%, if it's 4, 50%, and if it's 5 you'll get all for free!. Write a JS program that logs to the console, how much you will have to pay if your receipt is for $60. Every time you reload your page you should see a different result. Use a switch statement for this exercise.

	var luckyNumber = Math.floor(Math.random()* 6);
 var youpay = 60;
 var msg;
 
 switch(luckyNumber) {
 	case 0:
 		msg = 1
 		break; 
 	case 1:
 		msg = 0.1
 		break; 
 	case 2:
 		msg = 0.25
 		break; 
 	case 3:
 		msg = 0.35
 		break; 
 	case 4:
 		msg = 0.5
 		break; 
 	case 5:
 		msg = 0
 		break; 
 }
 
 msg = youpay*(1-msg);
 console.log("Here's what you owe: $"+msg);
  1. Suppose you have been assigned a task and you need to convert a number to the name of a month. 1 should be shown as January, 2 as February and so on. Using a switch statement write the JS code that shows the names of the months in the console.

    var monthnum = Math.floor((Math.random()* 12)+1);
     var msg;
    
     switch(monthnum) {
     	case 1:
     		msg = "January"
     		break; 
     	case 2:
     		msg = "February"
     		break; 
     	case 3:
     		msg = "March"
     		break; 
     	case 4:
     		msg = "April"
     		break; 
     	case 5:
     		msg = "May"
     		break; 
     	case 6:
     		msg = "June"
     		break; 
     	case 7:
     		msg = "July"
     		break; 
     	case 8:
     		msg = "August"
     		break; 
     	case 9:
     		msg = "September"
     		break; 
     	case 10:
     		msg = "October"
     		break; 
     	case 11:
     		msg = "November"
     		break; 
     	case 12:
     		msg = "December"
     		break; 
     }
    
     console.log(monthnum+" is the month of "+msg);
  2. Finally, save your work, commit the changes to your git repository, and push to GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment