Files

51 lines
1.3 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Comparisons with the && (logical AND) operator
---
# Comparisons with the && (logical AND) operator
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
· _Combine the two if statements into one statement which will return `"Yes"` if `val` is less than or equal to `50` and greater than or equal to `25`. Otherwise, will return `"No"`._
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
The logical AND (`&&`) operator compares both statements and returns `true` only if both are true or can be converted to true (truthy).
### Hint 2
2018-10-12 15:37:13 -04:00
Remember that this effect can be also achieved by nesting `if` statements.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function testLogicalAnd(val) {
// Only change code below this line
if (val <= 50 && val >= 25) {
return "Yes";
2018-10-12 15:37:13 -04:00
}
// Only change code above this line
return "No";
}
// Change this value to test
testLogicalAnd(10);
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
The function first evaluates `if` the condition `val <= 50` evaluates to `true` converting `val` to a number if necessary, then does the same with `val >=25` because of the logical AND (`&&`) operator; if both return true, the `return "Yes"` statement is executed.
#### Relevant Links
2018-10-12 15:37:13 -04:00
- ["Logical operators" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)
</details>