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

My solution & questions

I noticed that in this video Dave use the Math.floor method. Is there going to be a problem in my solution because I use Math.round & not +1 in the object randomIt ?

var input = prompt("Insert a number"); var input2 = prompt("Insert a number bigger than the first one "); alert("You will get a number from " + input + " to " + input2 ); var randomIt = (Math.random() * parseInt(input2 - input) ) + parseInt(input) alert( Math.round(randomIt) );

Also I want to know how to improve my code so the users can insert any numbers instead of inserting the staring number and a bigger one than it....

1 Answer

Steven Parker
Steven Parker
243,656 Points

Math.floor just discards the fractional portion, so it only makes the value smaller. But Math.round can make the value smaller or larger, so as you noticed you need different calculations to insure the result is within the desired range.

Even then, the random distribution would be affected, and the chances of producing either the largest or smallest number of the range would only be half as likely as the other values in the range. Depending on how the result is used, this may not be a "problem", but could be if you were modeling something that should have an even distribution like a dice roll.