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
|
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);
|
|
|
|
```
|
|
|
|
|
|
|
|
`chainToSwitch("bob")` should be "Marley"
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(chainToSwitch('bob') === 'Marley');
|
|
|
|
```
|
|
|
|
|
|
|
|
`chainToSwitch(42)` should be "The Answer"
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(chainToSwitch(42) === 'The Answer');
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`chainToSwitch(1)` should be "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
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`chainToSwitch(99)` should be "Missed me by this much!"
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(chainToSwitch(99) === 'Missed me by this much!');
|
|
|
|
```
|
|
|
|
|
|
|
|
`chainToSwitch(7)` should be "Ate Nine"
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(chainToSwitch(7) === 'Ate Nine');
|
|
|
|
```
|
|
|
|
|
|
|
|
`chainToSwitch("John")` should be "" (empty string)
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(chainToSwitch('John') === '');
|
|
|
|
```
|
|
|
|
|
|
|
|
`chainToSwitch(156)` should be "" (empty string)
|
|
|
|
|
|
|
|
```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
|
|
|
}
|
|
|
|
```
|