Comparisons with the Logical Or Operator
Solutions
Solution 1 (Click to Show/Hide)
if (val < 10 || val > 20) {
return "Outside";
}
Code Explanation
- The above code will return “Outside” only if
val
is between 10 and 20 (inclusive).
if (val < 10 || val > 20) {
return "Outside";
}
val
is between 10 and 20 (inclusive).Solution:
function testLogicalOr(val) {
// Only change code below this line
if (val > 20 || val < 10) {
return “Outside”;
}
// Only change code above this line
return “Inside”;
}
// Change this value to test
testLogicalOr(15);
Solution:
If we want to find a value between 2( here 10 and 20 inclusive) numbers the && operator is a must or else all conditions used to test values above 20 and below 10 wont satisfy .
But if we use && operator we wont be using the || operator which must be used at least once in this exercise.
Below solution will satisfy all test conditions but there is a mismatch in the design of the problem itself - BETWEEN cannot be used when we are learning OR operator.
Waiting for comments to check if my understanding is correct.
Solution:
function testLogicalOr(val) {
// Only change code below this line
if ((val > 10 || val == 10) && val <= 20 ) {
return “Inside”;
}
// Only change code above this line
return “Outside”;
}
// Change this value to test
testLogicalOr(10);