2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244dd
2021-02-06 04:42:36 +00:00
title: Selecting from Many Options with Switch Statements
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/c4mv4fm'
forumTopicId: 18277
2021-01-13 03:31:00 +01:00
dashedName: selecting-from-many-options-with-switch-statements
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
If you have many options to choose from, use a < dfn > switch</ dfn > statement. A `switch` statement tests a value and can have many < dfn > case</ dfn > statements which define various possible values. Statements are executed from the first matched `case` value until a `break` is encountered.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here is an example of a `switch` statement:
2020-02-18 01:40:55 +09:00
```js
switch(lowercaseLetter) {
case "a":
console.log("A");
break;
case "b":
console.log("B");
break;
}
```
2021-02-06 04:42:36 +00:00
`case` values are tested with strict equality (`===` ). The `break` tells JavaScript to stop executing statements. If the `break` is omitted, the next statement will be executed.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Write a switch statement which tests `val` and sets `answer` for the following conditions:
`1` - "alpha"
2020-12-16 00:37:30 -07:00
`2` - "beta"
2021-02-06 04:42:36 +00:00
`3` - "gamma"
2020-12-16 00:37:30 -07:00
`4` - "delta"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`caseInSwitch(1)` should have a value of "alpha"
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(caseInSwitch(1) === 'alpha');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`caseInSwitch(2)` should have a value of "beta"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(caseInSwitch(2) === 'beta');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`caseInSwitch(3)` should have a value of "gamma"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(caseInSwitch(3) === 'gamma');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`caseInSwitch(4)` should have a value of "delta"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(caseInSwitch(4) === 'delta');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not use any `if` or `else` statements
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(!/else/g.test(code) || !/if/g.test(code));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should have at least 3 `break` statements
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/break/g).length > 2);
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function caseInSwitch(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
caseInSwitch(1);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```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";
}
return answer;
}
```