Use of || comparison and < and >

Tell us what’s happening:
I don’t understand why the numbers returned through || are between 5 and 10 inclusive. Shouldn’t I use <= instead of < to include the named number in the statement? I could solve the challenge alright but I didn’t understand that.

Your code so far


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

if (val > 20 || val < 10) {
  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/85.0.4183.102 Safari/537.36.

Challenge: Comparisons with the Logical Or Operator

Link to the challenge:

About the code in the example to which you are referring…

if (num > 10 || num < 5) {
  return "No";
}
return "Yes";

… it says:

… will return “Yes” only if num is between 5 and 10 (5 and 10 included). The same logic can be written as:

So, It is saying that if it is 5, it will fail the if statement and return “Yes”. That is correct. I think you may be thinking backwards. They are not saying that the num > 10 is inclusive - its opposite is. In other works that checks if num is greater than 10 non-inclusive, so its opposite is less than 10 inclusive - the opposite of > is <=.

So, the combined logic in that if (num > 10 || num < 5) { is seeing if num is greater than 10 or less than 5, non-inclusive. So, the only thing that gets to the second return is numbers between 10 and 5 _inclusive.

Does that help? Yeah, it is a bit of a brain twister sometimes.

1 Like

I can see how that phrasing might be a little confusing. Instead of saying:

will return “Yes” only if num is between 5 and 10 (5 and 10 included)

perhaps it would have been clearer to say:

will return “No” only if num is not between 5 and 10 (5 and 10 not included)

They are both true statements about the above logic, but since the code as written starts with the “no” logic, it may be confusing. Personally, I would have written the function with the opposite logic because of that - that’s what you’re testing, the affirmative - but they would both do the same thing.

1 Like

@kevinSmith Thank you very much, your explanation is crystal clear. I had understood the first one but the second made it even more evident. You’re perfectly right. So thank you!

1 Like