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) Working With Numbers The Random Challenge

Kirby Abogaa
Kirby Abogaa
3,058 Points

Here's my solution.

Hi Guys!

This is my answer. Spent almost an hour thinking.

Hope it helps! Let me know if there is something wrong.

This is for the hard part where you get two values and create a random number between those two.

Collect your numbers. Make sure the second number is higher than the first so we can use it as a parameter for our random number. Parse the collected value to an integer and store in a var

var numLow = parseInt ( prompt ( "PLease enter the first number." ) ); 

var numHigh = parseInt ( prompt ( "PLease enter a second number higher than the first." ) );

Create a random number.

var result = Math.random() * ( numHigh - numLow ) + numLow;

This will generate a number between zero and the difference of the highest number to the lowest. It will ensure that the final result will not exceed the highest number.

We add back the lowest number to the random number generated to make sure that the final result is always above or equal to it.

You may use the other Math Object functions such as Math.ceil if you want the result to be a whole number.