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 trialMichelle Dobbs
Full Stack JavaScript Techdegree Student 1,060 PointsPlease explain the *10 and +1 in this problem
When writing code to generate a random whole number between 1 and 10, why am I multiplying by 10 and then adding 1? It is working each time it is run, of course, I am just trying to understand why it is working. Thanks in advance!
var random = Math.floor(Math.random() *10)+1;
2 Answers
KRIS NIKOLAISEN
54,967 PointsMath.random() generates a random number between 0 (inclusive), and 1 (exclusive)
Multiplying by ten makes the number between 0 (inclusive), and 10 (exclusive)
Taking the floor makes the number between 0 (inclusive) and 9 (inclusive)
Adding 1 makes the number between 1 (inclusive) and 10 (inclusive)
So it's a formula for generating a random number between 1 and 10
Michelle Dobbs
Full Stack JavaScript Techdegree Student 1,060 PointsThanks! You are awesome!