2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244de
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/c3JvVfg'
|
|
|
|
|
forumTopicId: 16653
|
|
|
|
|
localeTitle: 在 Switch 语句中添加默认选项
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
在<code>switch</code>语句中你可能无法用 case 来指定所有情况,这时你可以添加 default 语句。当再也找不到 case 匹配的时候 default 语句会执行,非常类似于 if/else 组合中的 else 语句。
|
|
|
|
|
<code>default</code>语句应该是最后一个 case。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
switch (num) {
|
|
|
|
|
case value1:
|
|
|
|
|
statement1;
|
|
|
|
|
break;
|
|
|
|
|
case value2:
|
|
|
|
|
statement2;
|
|
|
|
|
break;
|
|
|
|
|
...
|
|
|
|
|
default:
|
|
|
|
|
defaultStatement;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
写一个 switch 语句,根据下面的条件来设置<code>answer</code>的switch语句:<br><code>"a"</code> - "apple"<br><code>"b"</code> - "bird"<br><code>"c"</code> - "cat"<br><code>default</code> - "stuff"
|
|
|
|
|
</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>switchOfStuff("a")</code>应该有一个值为 "apple"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff("a") === "apple");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>switchOfStuff("b")</code>应该有一个值为 "bird"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff("b") === "bird");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>switchOfStuff("c")</code>应该有一个值为 "cat"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff("c") === "cat");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>switchOfStuff("d")</code>应该有一个值为 "stuff"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff("d") === "stuff");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>switchOfStuff(4)</code>应该有一个值为 "stuff"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff(4) === "stuff");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 不能使用任何<code>if</code>或<code>else</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(!/else/g.test(code) || !/if/g.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该有一个<code>default</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(switchOfStuff("string-to-trigger-default-case") === "stuff");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该有至少 3 个<code>break</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/break/g).length > 2);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function switchOfStuff(val) {
|
|
|
|
|
var answer = "";
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
|
switchOfStuff(1);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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 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;
|
|
|
|
|
}
|
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>
|