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

Lioz Elinger
Lioz Elinger
4,766 Points

I didn't understand the second part of the challenge at all.

I've tried many times to hear it again and again and I just don't get it. Is there any resource or someone who could explain it to me? the math was very confusing for me.

2 Answers

Tom Settle
Tom Settle
5,127 Points

Important to note the Math.Random() returns a float between 0(inclusive) and 1(exclusive). So similar to the first part of the challenge; the range (topNumber - bottomNumber + 1) represents the total number of numbers that can be chosen (ex. topNumber = 10; bottomNumber = 5 // range of numbers = 5, 6, 7, 8, 9, 10 is 6 choices, hence the + 1). This is then multiplied by the Math.Random() and then the floor is taken to get a value. The lowest possible value that could be calculated here is a 0 so to ensure that the result stays in the range you then add it to bottomNumber (ie 5) to return 5; The highest possible would be, say Math.Random() = .99 the floor of this multiplied by 6 would be 5 then added to 5 again returns 10.

Hope this helps!

This is really helpful,thank you!

Lioz Elinger
Lioz Elinger
4,766 Points

It does helps, although it takes some time for me to feel comfortable with that.

Thank you Tom.