2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244d8
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 逻辑与运算符
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cvbRVtr'
|
|
|
|
|
forumTopicId: 16799
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: comparisons-with-the-logical-and-operator
|
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
|
|
|
|
有时你需要在一次判断中做多个操作。 当且仅当<dfn>运算符</dfn>的左边和右边都是 true,<dfn>逻辑与</dfn>运算符(`&&`)才会返回`true`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
同样的效果可以通过 if 语句的嵌套来实现:
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (num > 5) {
|
|
|
|
|
if (num < 10) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
只有当 `num` 的值大于 `5` 并且小于`10` 时才会返回 `Yes`。 相同的逻辑可被写为:
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (num > 5 && num < 10) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
```
|
|
|
|
|
|
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
|
|
|
|
请使用 `&&` 运算符把两个 if 语句合并为一个 if 语句,如果 `val` 小于或等于`50` 并且大于或等于 `25` 时,返回 `Yes`。 否则,将返回`No`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你应该使用 `&&` 运算符一次。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/&&/g).length === 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你应该只有一个 `if` 表达式。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/if/g).length === 1);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(0)` 应该返回字符串 `No`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(testLogicalAnd(0) === 'No');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(24)` 应该返回字符串 `No`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(24) === 'No');
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(25)` 应该返回字符串 `Yes`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(25) === 'Yes');
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(30)` 应该返回字符串 `Yes`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(30) === 'Yes');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(50)` 应该返回字符串 `Yes`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(50) === 'Yes');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(51)` 应该返回字符串 `No`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(51) === 'No');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(75)` 应该返回字符串 `No`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(testLogicalAnd(75) === 'No');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`testLogicalAnd(80)` 应该返回字符串 `No`
|
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(testLogicalAnd(80) === 'No');
|
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 testLogicalAnd(val) {
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
|
if (val) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
return "No";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testLogicalAnd(10);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
function testLogicalAnd(val) {
|
|
|
|
|
if (val >= 25 && val <= 50) {
|
|
|
|
|
return "Yes";
|
|
|
|
|
}
|
|
|
|
|
return "No";
|
|
|
|
|
}
|
|
|
|
|
```
|