2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Adding a Default Option in Switch Statements
|
|
|
|
---
|
|
|
|
|
|
|
|
# Adding a Default Option in Switch Statements
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
|
|
|
Adding a default option makes sure that in case your variable doesn't match any of the options, the default will be used.
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
function switchOfStuff(val) {
|
|
|
|
var answer = "";
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
switch (val) {
|
|
|
|
case "a":
|
|
|
|
answer = "apple";
|
|
|
|
break;
|
|
|
|
case "b":
|
|
|
|
answer = "bird";
|
|
|
|
break;
|
|
|
|
case "c":
|
|
|
|
answer = "cat";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
answer = "stuff";
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
return answer;
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
2019-07-24 00:59:27 -07:00
|
|
|
```
|
|
|
|
</details>
|