2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244da
|
|
|
|
|
title: Introducing Else Statements
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cek4Efq'
|
|
|
|
|
forumTopicId: 18207
|
|
|
|
|
localeTitle: 介绍 else 语句
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
当<code>if</code>语句的条件为真,大括号里的代码执行,那如果条件为假呢?正常情况下什么也不会发生。使用else语句,可以执行当条件为假时相应的代码。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (num > 10) {
|
|
|
|
|
return "Bigger than 10";
|
|
|
|
|
} else {
|
|
|
|
|
return "10 or Less";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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>语句。
|
|
|
|
|
</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>if</code>表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/if/g).length === 1);
|
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>testElse(4)</code>应该返回 "5 or Smaller"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(4) === "5 or Smaller");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testElse(5)</code>应该返回 "5 or Smaller"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(5) === "5 or Smaller");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testElse(6)</code>应该返回 "Bigger than 5"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(6) === "Bigger than 5");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testElse(10)</code>应该返回 "Bigger than 5"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(10) === "Bigger than 5");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 不要修改上面和下面的代码。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/var result = "";/.test(code) && /return result;/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function testElse(val) {
|
|
|
|
|
var result = "";
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
if (val > 5) {
|
|
|
|
|
result = "Bigger than 5";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val <= 5) {
|
|
|
|
|
result = "5 or Smaller";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
|
testElse(4);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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 testElse(val) {
|
|
|
|
|
var result = "";
|
|
|
|
|
if(val > 5) {
|
|
|
|
|
result = "Bigger than 5";
|
|
|
|
|
} else {
|
|
|
|
|
result = "5 or Smaller";
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
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>
|