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

I solved the challenge another way, and got the desired results. Is it still correct?

function randomNumber (num1, num2) { var numberThree = num1 * num2; }

numberThree = Math.floor(Math.random(numberThree) * (6 - 1 + 1)) + 1; alert(numberThree);

1 Answer

function randomNumber (num1, num2) { 
    var numberThree = num1 * num2; 
}
numberThree = Math.floor(Math.random(numberThree) * (6 - 1 + 1)) + 1; 
alert(numberThree);

The "correct" solution here is:

function getRandomNumber (lower, upper) {
   return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}

There are a few problems with your solution. You are not returning anything from inside your function. This means that numberThree when called outside the function is undefined. It is only defined inside the function (see scope to understand this). So the first step might be to move the bottom two lines inside the function braces.

If you do this there is still a problem. Math.random() does not take any input. It returns a number between 0 and 1. This number needs to be multiplied by the inputs (num1 and num2) in order to return a range of numbers between them. Your function will only return a number between 1 and 6 regardless of what numbers are sent to the function. I hope this helps.