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

Mason Barton
seal-mask
.a{fill-rule:evenodd;}techdegree
Mason Barton
Full Stack JavaScript Techdegree Student 5,656 Points

Is this method fine?

function getRandomNumber( lower, upper ) { if ( isNan = true) { throw new Error('NOT A NUMBER'); }

I did this and got the error that I needed to get, but he mentioned needing to use ||, which I didnt do, nor could I figure out how

1 Answer

Unfortunately, no. Copying your function into multiple lines to see it better:

function getRandomNumber( lower, upper ) {
  if ( isNan = true) {
    throw new Error('NOT A NUMBER');
  }

Several things here:

  1. You haven't closed your function with a curly brace. Hopefully this is just a copying omission.
  2. You're not using either argument (lower and upper) you pass into the function. If you don't need them, don't pass them in.
  3. This function will always throw a NOT A NUMBER error, because your if statement isn't evaluating anything, it's assigning something. To evaluate something, you have to use double- or triple-equals: a == b or a === b. This compares a and b to each other. If you say a = b, you are assigning the value of b as the value of a, the same way you'd say a = 3 to set the variable's value. Assignments of positive values always return true, so your if statement will always run.
  4. isNaN first off needs a capital N at the end, and also is not a standalone value, it's a function. You have to call it and you have to pass a value to it: isNaN(3) or isNaN(a). If you do that, it will return true or false. Extra note: since the isNaN() function itself returns true or false, you do not have to compare it to true at all in the if function. You'd usually see this written like so: if ( isNaN(a) ) { /* whatever */ } since isNaN returning true or false will give the if condition all it needs to evaluate whether to run or not.

Hope that all makes sense! Cheers.