2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244d8
2021-02-06 04:42:36 +00:00
title: Comparisons with the Logical And Operator
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cvbRVtr'
forumTopicId: 16799
2021-01-13 03:31:00 +01:00
dashedName: comparisons-with-the-logical-and-operator
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Sometimes you will need to test more than one thing at a time. The < dfn > logical and</ dfn > operator (`&&` ) returns `true` if and only if the < dfn > operands</ dfn > to the left and right of it are true.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The same effect could be achieved by nesting an if statement inside another if:
2020-04-29 18:29:13 +08:00
```js
if (num > 5) {
if (num < 10 ) {
return "Yes";
}
}
return "No";
```
2021-02-06 04:42:36 +00:00
will only return "Yes" if `num` is greater than `5` and less than `10` . The same logic can be written as:
2020-04-29 18:29:13 +08:00
```js
if (num > 5 & & num < 10 ) {
return "Yes";
}
return "No";
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Replace the two if statements with one statement, using the && operator, which will return `"Yes"` if `val` is less than or equal to `50` and greater than or equal to `25` . Otherwise, will return `"No"` .
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
You should use the `&&` operator once
2020-12-16 00:37:30 -07:00
```js
assert(code.match(/& & /g).length === 1);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should only have one `if` statement
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(code.match(/if/g).length === 1);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testLogicalAnd(0)` should return "No"
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(testLogicalAnd(0) === 'No');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testLogicalAnd(24)` should return "No"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(24) === 'No');
```
2021-02-06 04:42:36 +00:00
`testLogicalAnd(25)` should return "Yes"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(25) === 'Yes');
```
2021-02-06 04:42:36 +00:00
`testLogicalAnd(30)` should return "Yes"
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(30) === 'Yes');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`testLogicalAnd(50)` should return "Yes"
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(50) === 'Yes');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testLogicalAnd(51)` should return "No"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(51) === 'No');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testLogicalAnd(75)` should return "No"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testLogicalAnd(75) === 'No');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testLogicalAnd(80)` should return "No"
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(testLogicalAnd(80) === 'No');
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01: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 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function testLogicalAnd(val) {
if (val >= 25 & & val < = 50) {
return "Yes";
}
return "No";
}
```