Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

115 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 56533eb9ac21ba0edf2244dd
title: 使用 Switch 语句从许多选项中进行选择
challengeType: 1
videoUrl: 'https://scrimba.com/c/c4mv4fm'
forumTopicId: 18277
dashedName: selecting-from-many-options-with-switch-statements
---
# --description--
如果你有非常多的选项需要选择,可以使用 switch 语句。根据不同的参数值会匹配上不同的 case 分支,语句会从第一个匹配的 case 分支开始执行,直到碰到 break 就结束。
这是一个伪代码案例:
```js
switch(lowercaseLetter) {
case "a":
console.log("A");
break;
case "b":
console.log("B");
break;
}
```
测试`case`值使用严格相等运算符进行比较break 关键字告诉 JavaScript 停止执行语句。如果没有 break 关键字,下一个语句会继续执行。
# --instructions--
写一个测试`val`的 switch 语句,并且根据下面的条件来设置不同的`answer`
`1`- "alpha"
`2` - "beta"
`3`- "gamma"
`4` - "delta"
# --hints--
`caseInSwitch(1)` 应该有一个值为 "alpha"。
```js
assert(caseInSwitch(1) === 'alpha');
```
`caseInSwitch(2)` 应该有一个值为 "beta"。
```js
assert(caseInSwitch(2) === 'beta');
```
`caseInSwitch(3)` 应该有一个值为 "gamma"。
```js
assert(caseInSwitch(3) === 'gamma');
```
`caseInSwitch(4)` 应该有一个值为 "delta"。
```js
assert(caseInSwitch(4) === 'delta');
```
不能使用任何`if``else`表达式。
```js
assert(!/else/g.test(code) || !/if/g.test(code));
```
你应该有至少 3 个`break`表达式。
```js
assert(code.match(/break/g).length > 2);
```
# --seed--
## --seed-contents--
```js
function caseInSwitch(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
caseInSwitch(1);
```
# --solutions--
```js
function caseInSwitch(val) {
var answer = "";
switch(val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
}
return answer;
}
```