Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge Solution

Is my code correct? Need help!

// Random Number Generator

function randomNumber (upper,lower) {

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

return newNumber;

alert(newNumber); }

alert(randomNumber(69,1));

alert(randomNumber(100,5));

alert(randomNumber(70,2));

//My code on Workspace doesn't have all this spacing just added it so that it is easier to read

2 Answers

Your code looks pretty good - just one thing to point out. In your function, you have alert(newNumber); after the return statement - while it won't cause an error, that line will never run. Once your code hits the return statement, it doesn't go any further in the function. It works fine, though - you may just want to take that line out. Otherwise, it's looking good and it worked fine when I tested it. Happy coding :)

function randomNumber (upper,lower) {
    var newNumber = Math.floor(Math.random() * (upper - lower + 1)) + lower;
    return newNumber;
    alert(newNumber); //this line will never run - you can safely delete it
}

alert(randomNumber(69,1));
alert(randomNumber(100,5));
alert(randomNumber(70,2));
Salvador Cervantes
Salvador Cervantes
10,898 Points

Yes it works,

You can actually test this your self to see if it works by hitting on your keyboard

"ctrl" + "Shift" + "i" then you can navigate to the console. Paste your code and press "enter" then will then let you know if it worked or not. I did it my self and i got random numbers!!!

Have fun!