Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,673 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
seal-mask
.a{fill-rule:evenodd;}techdegree
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