Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge, Part II

What if both inputs are not numbers? is the OR-operator really enough?

Shouldnt we use: if ( (isNan(upper) OR isNAN(lower)) OR (isNan(upper) AND isNAN(lower) ))

any thoughts?

2 Answers

Antti Lylander
Antti Lylander
9,686 Points

One || operator is enough. If the first test is true, the second doesn't even get evaluated. It simply does not matter anymore because we already know that either of the values is not a number and we need to throw an error.

true || true -> true
false || false -> false
true || false -> true
false || true -> true
Luc de Brouwer
Luc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 Points

The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise. So when one value evaluates to true, the rest of the condition will be ignored, since the first condition is met. So if either are false, the condition evaluaties also to true, you see why we use the logical OR?

If NaN(first) || NaN(second) is good enough since if they are both false, the condition will also be true