2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244db
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 介绍 else if 语句
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/caeJ2hm'
|
|
|
|
forumTopicId: 18206
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: introducing-else-if-statements
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
如果你有多个条件语句,你可以通过 `else if` 语句把 `if` 语句链起来。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
if (num > 15) {
|
|
|
|
return "Bigger than 15";
|
|
|
|
} else if (num < 5) {
|
|
|
|
return "Smaller than 5";
|
|
|
|
} else {
|
|
|
|
return "Between 5 and 15";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
使用 `else if` 实现同样的效果。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你应该至少有两个 `else` 表达式。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(code.match(/else/g).length > 1);
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你应该至少有两个 `if` 表达式。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(code.match(/if/g).length > 1);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
应该关闭每一个 `if else` 代码块。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(
|
|
|
|
code.match(
|
|
|
|
/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s+if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/
|
|
|
|
)
|
|
|
|
);
|
2020-12-16 07:37:30 +00:00
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testElseIf(0)` 应该返回字符串 `Smaller than 5`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(testElseIf(0) === 'Smaller than 5');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testElseIf(5)` 应该返回字符串 `Between 5 and 10`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(testElseIf(5) === 'Between 5 and 10');
|
2020-12-16 07:37:30 +00:00
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testElseIf(7)` 应该返回字符串 `Between 5 and 10`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(testElseIf(7) === 'Between 5 and 10');
|
2020-12-16 07:37:30 +00:00
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testElseIf(10)` 应该返回字符串 `Between 5 and 10`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(testElseIf(10) === 'Between 5 and 10');
|
2020-12-16 07:37:30 +00:00
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testElseIf(12)` 应该返回字符串 `Greater than 10`
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(testElseIf(12) === 'Greater than 10');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function testElseIf(val) {
|
|
|
|
if (val > 10) {
|
|
|
|
return "Greater than 10";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val < 5) {
|
|
|
|
return "Smaller than 5";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Between 5 and 10";
|
|
|
|
}
|
|
|
|
|
|
|
|
testElseIf(7);
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function testElseIf(val) {
|
|
|
|
if(val > 10) {
|
|
|
|
return "Greater than 10";
|
|
|
|
} else if(val < 5) {
|
|
|
|
return "Smaller than 5";
|
|
|
|
} else {
|
|
|
|
return "Between 5 and 10";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|