2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244da
|
|
|
|
|
title: Introducing Else Statements
|
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
|
|
|
|
localeTitle: 介绍其他声明
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id="description">当<code>if</code>语句的条件为真时,将执行其后面的代码块。当那个条件是假的时候怎么办?通常什么都不会发生。使用<code>else</code>语句,可以执行备用代码块。 <blockquote> if(num> 10){ <br>返回“大于10”; <br> } else { <br>返回“10或更少”; <br> } </blockquote></section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id="instructions">将<code>if</code>语句组合到单个<code>if/else</code>语句中。 </section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: 您应该只在编辑器中有一个<code>if</code>语句
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/if/g).length === 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: 你应该使用<code>else</code>语句
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/else/g.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>testElse(4)</code>应返回“5或更小”
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(4) === "5 or Smaller");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>testElse(5)</code>应返回“5或更小”
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(5) === "5 or Smaller");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>testElse(6)</code>应该返回“大于5”
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(6) === "Bigger than 5");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>testElse(10)</code>应该返回“大于5”
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testElse(10) === "Bigger than 5");
|
2018-10-10 18:03:03 -04: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'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|
|
|
|
|
</section>
|