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

code output is not as expected -_-

var lower = parseInt (prompt ('Enter Min Number') ) ;

var upper = parseInt (prompt ('Enter Max Number') ) ;

function getRandomNumber (lower , upper) { if (upper > lower){

var randomNumber = Math.floor(Math.random() * (upper - lower + 1)) + lower;

return randomNumber;

}else {

var randomNumber =  Math.floor(Math.random() * (lower - upper + 1)) + upper; 

return randomNumber ; }

}

alert(getRandomNumber);

can someone please let me know why is my output he actual function details themselves ?

2 Answers

Dan Weru
Dan Weru
47,649 Points

Hey, you're doing great. You just need to tweak your code a little. You need to add the opening and closing parenthesis () after getRandomNumber. Doing so makes the function to execute.

Instead of this

  alert(getRandomNumber);

Write this

  alert(getRandomNumber());

Or write

  var result = getRandomNumber();
  alert(result);

seems like my console is always displaying NAN as a result for some reason !

Bret Lynn
Bret Lynn
7,037 Points

Here's how I did mine...seems to work. I suppose I cheated a little....I used MDN to find help.

function getRandomArbitrary (min, max) { return Math.floor (Math.random() * (max - min) + min); }

alert (getRandomArbitrary(20, 100));

you should call the function with parentheses after the function's name in the alert like this: alert(getRandomNumber()) ;

thanks