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

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!