2018-10-04 13:37:37 +00:00
---
id: 56533eb9ac21ba0edf2244d8
title: Comparisons with the Logical And Operator
challengeType: 1
2019-02-14 17:24:02 +00:00
videoUrl: 'https://scrimba.com/c/cvbRVtr'
2019-07-31 18:32:23 +00:00
forumTopicId: 16799
2021-01-13 02:31:00 +00:00
dashedName: comparisons-with-the-logical-and-operator
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
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.
2018-10-04 13:37:37 +00:00
The same effect could be achieved by nesting an if statement inside another if:
2019-05-17 13:20:30 +00:00
```js
if (num > 5) {
if (num < 10 ) {
return "Yes";
}
}
return "No";
```
2021-03-03 00:12:12 +00:00
will only return `Yes` if `num` is greater than `5` and less than `10` . The same logic can be written as:
2019-05-17 13:20:30 +00:00
```js
if (num > 5 & & num < 10 ) {
return "Yes";
}
return "No";
```
2020-11-27 18:02:05 +00:00
# --instructions--
2021-03-03 00:12:12 +00:00
Replace the two if statements with one statement, using the `&&` operator, which will return the string `Yes` if `val` is less than or equal to `50` and greater than or equal to `25` . Otherwise, will return the string `No` .
2020-11-27 18:02:05 +00:00
# --hints--
You should use the `&&` operator once
```js
assert(code.match(/& & /g).length === 1);
```
You should only have one `if` statement
```js
assert(code.match(/if/g).length === 1);
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(0)` should return the string `No`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(0) === 'No');
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(24)` should return the string `No`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(24) === 'No');
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(25)` should return the string `Yes`
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(25) === 'Yes');
2018-10-04 13:37:37 +00:00
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(30)` should return the string `Yes`
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(30) === 'Yes');
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
`testLogicalAnd(50)` should return the string `Yes`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(50) === 'Yes');
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(51)` should return the string `No`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(51) === 'No');
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(75)` should return the string `No`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(75) === 'No');
```
2021-03-03 00:12:12 +00:00
`testLogicalAnd(80)` should return the string `No`
2020-11-27 18:02:05 +00:00
```js
assert(testLogicalAnd(80) === 'No');
```
# --seed--
## --seed-contents--
2018-10-04 13:37:37 +00:00
```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-11-27 18:02:05 +00:00
# --solutions--
2018-10-04 13:37:37 +00:00
```js
function testLogicalAnd(val) {
if (val >= 25 & & val < = 50) {
return "Yes";
}
return "No";
}
```