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 Solution

another solution Randomjs

Hello

im just wondering if my solution is also correct i have test and it seems to work just fine , but its different from the video , just wondering if my solution is correct ?

var UserInputMax = prompt('please enter a number max'); UserInputMax = parseInt(UserInputMax);

var UserInputMin = prompt('please enter a number min'); UserInputMin = parseInt(UserInputMin);

var total = Math.floor(Math.random() * (UserInputMax - UserInputMin)) + UserInputMin;

console.log('the user has enter from ' + UserInputMin + ' to ' + UserInputMax + ' and they GOT ' + total);

1 Answer

Steven Parker
Steven Parker
231,269 Points

This is almost the same as the video.

The main difference is logging to the console instead of writing to the document. Otherwise you just have one error:

var total = Math.floor(Math.random() * (UserInputMax - UserInputMin)) + UserInputMin;

To get an inclusive range you need to add one to the multiplier:

var total = Math.floor(Math.random() * (UserInputMax - UserInputMin + 1)) + UserInputMin;

the UserInputMax - UserInputMin + 1 , would make it only not reaching 0 is that correct ?

Steven Parker
Steven Parker
231,269 Points

We don't know if the user will enter 0 for either number. But whatever 2 numbers are given, if you want the random number to have a chance of reaching the larger one, you must add 1 to the multiplier.