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 Solution

Matthew Lee
Matthew Lee
3,910 Points

Unable to get result using alert

Hi guys, I've tried using the following codes but unable to get the alert with random number. I've also tried several options but couldn't get it to work. Would require some assistance here..

function randomNumber (lower, upper) { var random = Math.floor(Math.random() * (upper - lower + 1)) + lower;
return random; alert(random); } randomNumber (2,10);

3 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

any code you have in a function body that comes after the return statement is going to be ignored. hitting a return statement causes the function to exit. alert the call to the function with 2 and 10. it returns a random number, and that will be the argument to alert, and will be so alerted.

Your returning before the alert

arik
arik
5,791 Points
  1. I think it is because you are only calling the function --->randomNumber (2,10); without any command to print it out, if you want to use alert to print, I think you can write it ---> alert (randomNumber (2,10) );

  2. alert (random) inside the function won't be read since it comes after return, which will exit the function immediately.

  3. I think this code should work:

function randomNumber (lower, upper) { var random = Math.floor(Math.random() * (upper - lower + 1)) + lower; return random; } alert (randomNumber (2,10));