Need Clarifications on Comparisons with Logical OR Operator

I am stuck on this challenge. I have check the hint for this challenge and tried the solution but it did not pass the test. The last thread about this challenge dates back to Dec 2019 (quiet old already). I think its time to get additional inputs about this challenge. Hope to see the communities inputs.

  **Your code so far**

function testLogicalOr(val) {
// Only change code below this line

if (val > 0 || val < 19) {
  return "Outside";
}

// Only change code above this line
return "Inside";
}

testLogicalOr(15);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36

Challenge: Comparisons with the Logical Or Operator

Link to the challenge:

The issue with your code lies not with the logical OR. Here is the requirement lifted from the challenge:

Combine the two if statements into one statement which returns the string Outside if val is not between 10 and 20 , inclusive. Otherwise, return the string Inside .

Keyword there being inclusive .

Your code is saying :

if val is greater than 0 OR less than 19
return “Outside”, otherwise return “Inside”.

You’re saying bigger than 0 OR smaller than 19.

So it will fail the tests.

Fix that.

I have added the logical operator && in the code - it passed the test. Thank you for reading my post. Maybe there are other solutions to this challenge, hope others can share.

function testLogicalOr(val) {
// Only change code below this line

if (val < 10 || val > 20 && val > 14) {
return “Outside”;
}

// Only change code above this line
return “Inside”;
}

testLogicalOr(15);

That is not a correct solution. It happens to pass these tests, but it is not the correct logic. You should not use the && operator.

Thank you for the feedback, will continue to explore this one.

Isn’t this going to always evaluate to true?

3 Likes

Doesn’t it pass, if you remove && val > 14 ? I’m not seeing why it shouldn’t.

1 Like

@01albert

Forget about the AND && this lesson is focusing on using OR ||

Think carefully. You want it so that if the value is not between 10 and 20, return Outside. Otherwise return Inside.

So for 10, 11, 12…20 it is Inside and anything not in that range is Outside.

So, thinking “outside the box” of the range 10 through 20, try focusing on numbers that are under 10 or numbers that are over 20.

How would you expess “anything smaller than 10” in code?

How would you express “anything bigger than 20” in code?

Put those together with a logical OR ||.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.