Dragon Slayer - Declare your variables - Question about the Hint regarding the random matth


#1


Dragon Slayer! - Declare your variable


I don't have an issue with the code, I have a question about the Hint explanation. If I am reading this correctly, Math.floor(Math.random() * 5 + 1); would take a random number between 0 and 1, multiply it by 5, and then add 1. Then the Math.floor would round down. The Hint and instructions indicate that this will provide a number between 1 and 5, but wouldn't the range be actually be 1 to 6? If we assume that the random number by Math.random is 1, 1 * 5 + 1 would be 6. 6 is an round number so there is nothing for Math.Floor to do. Am I missing something here.?


How does this code work?

Math.floor(Math.random() * 5 + 1);
First we use Math.random() to create a random number from 0 up to 1. For example, 0.5
Then we multiply by 5 to make the random number from 0 up to 5. For example, 0.5 * 5 = 2.5
Next we use Math.floor() to round down to a whole number. For example, Math.floor( 2.5 ) = 2
Finally we add 1 to change the range from between 0 and 4 to between 1 and 5 (up to and including 5)


#2

a number between 0 and 1, so 0 and 1 are not included. so the highest possible value is: 0.999 (some more digits), which if you multiply by 5 will give you 4.99 (some more digits), plus one is 5.99, which floored is 5. 6 is impossible


#3

Ah I see, thanks for the quick response!


#4