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

Zulka Gavlovski
Zulka Gavlovski
1,322 Points

why this piece of code doesnt run

function getRandomNumber( lower, upper ) { var lowerNumber = isNaN (lower); var upperNumber = isNan (upper);

if ( lowerNumber = true || upperNumber = true ){ alert("please enter a real number"); }

return Math.floor(Math.random() * (upper - lower + 1)) + lower; }

console.log( getRandomNumber( 'nine', 24 ) );

2 Answers

Laurie Williams
Laurie Williams
10,174 Points

Here is a working version of your code. https://goo.gl/vg4MOJ

Firstly, when comparing a condition always use ===.

The formatting on isNaN was incorrect so the function didn't exist.

Also, if you can indent your code, it will be much easier to read.

function getRandomNumber (lower, upper) {
 var lowerNumber = isNaN(lower); 
 var upperNumber = isNaN(upper);

  if (lowerNumber === true || upperNumber === true) {
    alert("please enter a real number");
  }

return Math.floor(Math.random() * (upper - lower + 1)) + lower; }

console.log( getRandomNumber( 'nine', 24 ) );
Nicholas Vogel
Nicholas Vogel
12,318 Points

First, your isNaN's shouldn't have the space between that and the opening parenthesis (change to isNaN(lower), etc.). Second, you don't have a closing curly brace ( } ) after your first line. Third, your if else statement needs to be reformatted (switch the ; and the } ). Fourth, the ; in your return statement needs to be on the outside of the curly brace.

Phew. That should do it.