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 Solution

Lynn Mitchell
Lynn Mitchell
5,133 Points

Why is it that you don't have to say if (isNAN(upper) = true )?

Why is it that you don't have to say if (isNAN(upper) = true )? I tried to run this with

function getRandomNumber( lower, upper ) { if (isNAN(upper) =true || isNAN(lower) = true{ throw new Error("You must enter a number!"); }else{ return Math.floor(Math.random() * (upper - lower + 1)) + lower; }

I figured you would have to say if upper is true do this also what if you were asking if it is false? Then what would you do?

1 Answer

Steven Parker
Steven Parker
231,271 Points

First, a single equal sign ("=") is an assignment operator. A comparison is a double symbol ("==").

Comparison operators create a boolean result to indicate the outcome of the comparison. So you never need to compare any boolean value to "true", because it already represents that.

if (isNaN(upper) == true || isNaN(lower) == true)  // so this test...
if (isNaN(upper) || isNaN(lower))                  // does the same thing as THIS one

And to check if a boolean value is false you can use the "not" operator ("!"):

if (!isNaN(someValue))  // this expression is true when "isNaN" is false