2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244e0
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/c3JE8fy'
|
|
|
|
forumTopicId: 18266
|
|
|
|
localeTitle: 用一个 Switch 语句来替代多个 if else 语句
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
如果你有多个选项需要选择,<code>switch</code>语句写起来会比多个串联的<code>if</code>/<code>if else</code>语句容易些,譬如:
|
|
|
|
|
|
|
|
```js
|
|
|
|
if (val === 1) {
|
|
|
|
answer = "a";
|
|
|
|
} else if (val === 2) {
|
|
|
|
answer = "b";
|
|
|
|
} else {
|
|
|
|
answer = "c";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
可以被下面替代:
|
|
|
|
|
|
|
|
```js
|
|
|
|
switch(val) {
|
|
|
|
case 1:
|
|
|
|
answer = "a";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
answer = "b";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
answer = "c";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
把串联的<code>if</code>/<code>if else</code>语句改成<code>switch</code>语句。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 不要使用<code>else</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!/else/g.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 不要使用<code>if</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!/if/g.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 你应该有至少 4 个<code>break</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/break/g).length >= 4);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch("bob")</code>应该为 "Marley"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch("bob") === "Marley");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch(42)</code>应该为 "The Answer"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch(42) === "The Answer");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch(1)</code>应该为 "There is no #1"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: "assert(chainToSwitch(1) === \"There is no #1\");"
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch(99)</code>应该为 "Missed me by this much!"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch(99) === "Missed me by this much!");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch(7)</code>应该为 "Ate Nine"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch(7) === "Ate Nine");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch("John")</code>应该为 "" (empty string)。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch("John") === "");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>chainToSwitch(156)</code>应该为 "" (empty string)。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(chainToSwitch(156) === "");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function chainToSwitch(val) {
|
|
|
|
var answer = "";
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
chainToSwitch(7);
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
return answer;
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|