2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 56533eb9ac21ba0edf2244e0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Replacing If Else Chains with Switch
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you have many options to choose from, a < code > switch< / code >  statement can be easier to write than many chained < code > if< / code > /< code > else if< / code >  statements. The following:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< blockquote > if (val === 1) {< br >     answer = "a";< br > } else if (val === 2) {< br >     answer = "b";< br > } else {< br >     answer = "c";< br > }< / blockquote >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								can be replaced with:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< blockquote > switch(val) {< br >     case 1:< br >         answer = "a";< br >         break;< br >     case 2:< br >         answer = "b";< br >         break;< br >     default:< br >         answer = "c";< br > }< / blockquote >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Change the chained < code > if< / code > /< code > else if< / code >  statements into a < code > switch< / code >  statement.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: You should not use any < code > else</ code >  statements anywhere in the editor
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(!/else/g.test(code), 'You should not use any < code > else< / code >  statements anywhere in the editor');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: You should not use any < code > if</ code >  statements anywhere in the editor
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(!/if/g.test(code), 'You should not use any < code > if< / code >  statements anywhere in the editor');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: You should have at least four < code > break</ code >  statements
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(code.match(/break/g).length >= 4, 'You should have at least four < code > break< / code >  statements');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch("bob")</ code >  should be "Marley"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch("bob") === "Marley", '< code > chainToSwitch("bob")< / code >  should be "Marley"');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch(42)</ code >  should be "The Answer"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch(42) === "The Answer", '< code > chainToSwitch(42)< / code >  should be "The Answer"');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: "< code > chainToSwitch(1)</ code >  should be \"There is no #1 \""
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: "assert(chainToSwitch(1) === \"There is no #1 \", '< code > chainToSwitch(1)</ code >  should be \"There is no #1 \"');"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch(99)</ code >  should be "Missed me by this much!"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch(99) === "Missed me by this much!", '< code > chainToSwitch(99)< / code >  should be "Missed me by this much!"');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch(7)</ code >  should be "Ate Nine"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch(7) === "Ate Nine", '< code > chainToSwitch(7)< / code >  should be "Ate Nine"');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch("John")</ code >  should be "" (empty string)
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch("John") === "", '< code > chainToSwitch("John")< / code >  should be "" (empty string)');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > chainToSwitch(156)</ code >  should be "" (empty string)
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(chainToSwitch(156) === "", '< code > chainToSwitch(156)< / code >  should be "" (empty string)');
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function chainToSwitch(val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var answer = "";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  if (val === "bob") {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    answer = "Marley";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else if (val === 42) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    answer = "The Answer";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else if (val === 1) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    answer = "There is no #1 ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else if (val === 99) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    answer = "Missed me by this much!";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else if (val === 7) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    answer = "Ate Nine";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return answer;
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Change this value to test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								chainToSwitch(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function chainToSwitch(val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var answer = "";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  switch(val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case "bob":
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      answer = "Marley";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 42:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      answer = "The Answer";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 1:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      answer = "There is no #1 ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 99:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      answer = "Missed me by this much!";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 7:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      answer = "Ate Nine";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return answer;
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >