| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 56533eb9ac21ba0edf2244e0 | 
					
						
							|  |  |  | title: Replacing If Else Chains with Switch | 
					
						
							|  |  |  | challengeType: 1 | 
					
						
							| 
									
										
										
										
											2019-02-14 12:24:02 -05:00
										 |  |  | videoUrl: 'https://scrimba.com/c/c3JE8fy' | 
					
						
							| 
									
										
										
										
											2019-07-31 11:32:23 -07:00
										 |  |  | forumTopicId: 18266 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: replacing-if-else-chains-with-switch | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you have many options to choose from, a `switch` statement can be easier to write than many chained `if`/`else if` statements. The following: | 
					
						
							| 
									
										
										
										
											2019-05-17 06:20:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | if (val === 1) { | 
					
						
							|  |  |  |   answer = "a"; | 
					
						
							|  |  |  | } else if (val === 2) { | 
					
						
							|  |  |  |   answer = "b"; | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |   answer = "c"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | can be replaced with: | 
					
						
							| 
									
										
										
										
											2019-05-17 06:20:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | switch(val) { | 
					
						
							|  |  |  |   case 1: | 
					
						
							|  |  |  |     answer = "a"; | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   case 2: | 
					
						
							|  |  |  |     answer = "b"; | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   default: | 
					
						
							|  |  |  |     answer = "c"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --instructions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Change the chained `if`/`else if` statements into a `switch` statement. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --hints--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should not use any `else` statements anywhere in the editor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(!/else/g.test(code)); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should not use any `if` statements anywhere in the editor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(!/if/g.test(code)); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should have at least four `break` statements | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(code.match(/break/g).length >= 4); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch("bob")` should be the string `Marley` | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch('bob') === 'Marley'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch(42)` should be the string `The Answer` | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch(42) === 'The Answer'); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch(1)` should be the string `There is no #1` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch(1) === 'There is no #1'); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch(99)` should be the string `Missed me by this much!` | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch(99) === 'Missed me by this much!'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch(7)` should be the string `Ate Nine` | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch(7) === 'Ate Nine'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch("John")` should be `""` (empty string) | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch('John') === ''); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 16:12:12 -08:00
										 |  |  | `chainToSwitch(156)` should be `""` (empty string) | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(chainToSwitch(156) === ''); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | chainToSwitch(7); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```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
										 |  |  | } | 
					
						
							|  |  |  | ``` |