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 trialMatthew Lee
3,910 PointsUnable 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
Front End Web Development Techdegree Graduate 33,271 Pointsany 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.
Michael Clark
16,324 PointsYour returning before the alert
arik
5,791 PointsI 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) );
alert (random) inside the function won't be read since it comes after return, which will exit the function immediately.
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));