2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244dd
title: Selecting from Many Options with Switch Statements
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/c4mv4fm'
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
If you have many options to choose from, use a < code > switch< / code > statement. A < code > switch< / code > statement tests a value and can have many < code > case< / code > statements which define various possible values. Statements are executed from the first matched < code > case< / code > value until a < code > break< / code > is encountered.
Here is a < dfn > pseudocode< / dfn > example:
< blockquote > switch(num) {< br > case value1:< br > statement1;< br > break;< br > case value2:< br > statement2;< br > break;< br > ...< br > case valueN:< br > statementN;< br > break;< br > }< / blockquote >
< code > case< / code > values are tested with strict equality (< code > ===< / code > ). The < code > break< / code > tells JavaScript to stop executing statements. If the < code > break< / code > is omitted, the next statement will be executed.
< / section >
## Instructions
< section id = 'instructions' >
Write a switch statement which tests < code > val< / code > and sets < code > answer< / code > for the following conditions:< br > < code > 1< / code > - "alpha"< br > < code > 2< / code > - "beta"< br > < code > 3< / code > - "gamma"< br > < code > 4< / code > - "delta"
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > caseInSwitch(1)</ code > should have a value of "alpha"
2018-10-20 21:02:47 +03:00
testString: assert(caseInSwitch(1) === "alpha", '< code > caseInSwitch(1)< / code > should have a value of "alpha"');
2018-10-04 14:37:37 +01:00
- text: < code > caseInSwitch(2)</ code > should have a value of "beta"
2018-10-20 21:02:47 +03:00
testString: assert(caseInSwitch(2) === "beta", '< code > caseInSwitch(2)< / code > should have a value of "beta"');
2018-10-04 14:37:37 +01:00
- text: < code > caseInSwitch(3)</ code > should have a value of "gamma"
2018-10-20 21:02:47 +03:00
testString: assert(caseInSwitch(3) === "gamma", '< code > caseInSwitch(3)< / code > should have a value of "gamma"');
2018-10-04 14:37:37 +01:00
- text: < code > caseInSwitch(4)</ code > should have a value of "delta"
2018-10-20 21:02:47 +03:00
testString: assert(caseInSwitch(4) === "delta", '< code > caseInSwitch(4)< / code > should have a value of "delta"');
2018-10-04 14:37:37 +01:00
- text: You should not use any < code > if</ code > or < code > else</ code > statements
2018-10-20 21:02:47 +03:00
testString: assert(!/else/g.test(code) || !/if/g.test(code), 'You should not use any < code > if< / code > or < code > else< / code > statements');
2018-10-04 14:37:37 +01:00
- text: You should have at least 3 < code > break</ code > statements
2018-10-20 21:02:47 +03:00
testString: assert(code.match(/break/g).length > 2, 'You should have at least 3 < code > break< / code > statements');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function caseInSwitch(val) {
var answer = "";
// Only change code below this line
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
caseInSwitch(1);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function caseInSwitch(val) {
var answer = "";
switch(val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
}
2018-10-08 01:01:53 +01:00
return answer;
2018-09-30 23:01:58 +01:00
}
```
< / section >