2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244de
2021-02-06 04:42:36 +00:00
title: Adding a Default Option in 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/c3JvVfg'
forumTopicId: 16653
2021-01-13 03:31:00 +01:00
dashedName: adding-a-default-option-in-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
In a `switch` statement you may not be able to specify all possible values as `case` statements. Instead, you can add the `default` statement which will be executed if no matching `case` statements are found. Think of it like the final `else` statement in an `if/else` chain.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
A `default` statement should be the last case.
2020-04-29 18:29:13 +08:00
```js
switch (num) {
case value1:
statement1;
break;
case value2:
statement2;
break;
...
default:
defaultStatement;
break;
}
```
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 to set `answer` for the following conditions:
2020-12-16 00:37:30 -07:00
`"a"` - "apple"
`"b"` - "bird"
`"c"` - "cat"
`default` - "stuff"
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
`switchOfStuff("a")` should have a value of "apple"
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(switchOfStuff('a') === 'apple');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`switchOfStuff("b")` should have a value of "bird"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(switchOfStuff('b') === 'bird');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`switchOfStuff("c")` should have a value of "cat"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(switchOfStuff('c') === 'cat');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`switchOfStuff("d")` should have a value of "stuff"
2020-12-16 00:37:30 -07:00
```js
assert(switchOfStuff('d') === 'stuff');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`switchOfStuff(4)` should have a value of "stuff"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(switchOfStuff(4) === 'stuff');
```
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 use a `default` statement
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
```
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 switchOfStuff(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
switchOfStuff(1);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function switchOfStuff(val) {
var answer = "";
switch(val) {
case "a":
answer = "apple";
break;
case "b":
answer = "bird";
break;
case "c":
answer = "cat";
break;
default:
answer = "stuff";
}
return answer;
}
```