2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244d8
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cvbRVtr'
|
|
|
|
|
forumTopicId: 16799
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 逻辑与运算符
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
有时你需要在一次判断中做多个操作。当且仅当运算符的左边和右边都是<code>true</code>,<dfn>逻辑与</dfn> 运算符(<code>&&</code>)才会返回<code>true</code>。
|
|
|
|
|
同样的效果可以通过 if 语句的嵌套来实现:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (num > 5) {
|
|
|
|
|
if (num < 10) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
只有当<code>num</code>的值在 6 和 9 之间(包括 6 和 9)才会返回 "Yes"。相同的逻辑可被写为:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (num > 5 && num < 10) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
请使用逻辑与运算符把两个 if 语句合并为一个 if 语句,如果<code>val</code>小于或等于<code>50</code>并且大于或等于<code>25</code>,返回<code>"Yes"</code>。否则,将返回<code>"No"</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>&&</code>运算符一次。
|
|
|
|
|
testString: assert(code.match(/&&/g).length === 1,);
|
|
|
|
|
- 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>testLogicalAnd(0)</code>应该返回 "No"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(0) === "No");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(24)</code>应该返回 "No"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(24) === "No");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(25)</code>应该返回 "Yes"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(25) === "Yes");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(30)</code>应该返回 "Yes"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(30) === "Yes");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(50)</code>应该返回 "Yes"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(50) === "Yes");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(51)</code>应该返回 "No"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(51) === "No");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(75)</code>应该返回 "No"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(75) === "No");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>testLogicalAnd(80)</code>应该返回 "No"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(testLogicalAnd(80) === "No");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function testLogicalAnd(val) {
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
|
if (val) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
return "No";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
|
testLogicalAnd(10);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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 testLogicalAnd(val) {
|
|
|
|
|
if (val >= 25 && val <= 50) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
}
|
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>
|