randomRange should use both myMax and myMin, and return a random number in your range.
You cannot pass the test if you are only re-using the function ourRandomRange inside your randomRange formula. You need to write your own formula that uses the variables myMax and myMin. It will do the same job as using ourRandomRange, but ensures that you have understood the principles of the Math.floor() and Math.random() functions.
Math.random() generates our random number between 0 and â 0.9.
Before multiplying it, it resolves the part between parenthesis (myMax - myMin + 1) because of the grouping operator ( ).
The result of that multiplication is followed by adding myMin and then âroundedâ to the largest integer less than or equal to it (eg: 9.9 would result in 9)
If the values were myMin = 1, myMax= 10, one result could be the following:
Thank you for taking the time to walk through this step by step. I had been able to generate a code answer -but couldnât understand what the actual formula meant. Awesome. Again - thank you.
Yes, thank you for the explanation. It was easy to figure out what was required to pass the challenge, but I wasnât comfortable passing this without understanding the formula first. This explained it very logically, and I probably wouldnât have been able to figure it out on my own. â putting this explanation in my notes!
Lets start with `Math.floor(Math.random() * (6 + 1))â because we can agree that it returns 0-6.
However! If Math.random() returned 0 then Math.floor(0 * (6 + 1)) would return 0 which is outside of our given range of 2 through 6 : .
So the next logical step is to add two after all the multiplication has been executed. Which would look like: Math.floor(Math.random() * (myMax + 1)) + myMin
However! If Math.random() returned 6.99999 then Math.floor(6.99999 * (6 + 1)) + 2 would return 8! which is outside of our given range of 2 through 6. no good
SOOO we have to subtract the min from our max then add one to guarantee that the floor of random * our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a 0 .
Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.
In other words.
The range of 0 through 4 plus 2 is equal to the range 2 through 6.
Lets start with `Math.floor(Math.random() * (6 + 1))â because we can agree that it returns 0-6.
However! If Math.random() returned 0 then Math.floor(0 * (6 + 1)) would return 0 which is outside of our given range of 2 through 6 : .
So the next logical step is to add two after all the multiplication has been executed. Which would look like: Math.floor(Math.random() * (myMax + 1)) + myMin
However! If Math.random() returned 6.99999 then Math.floor(6.99999 * (6 + 1)) + 2 would return 8! which is outside of our given range of 2 through 6. no good
SOOO we have to subtract the min from our max then add one to guarantee that the floor of random * our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a 0 .
Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.
In other words.
The range of 0 through 4 plus 2 is equal to the range 2 through 6.
Iâm finding it very frustrating to have to come to the âHintsâ section to learn the skills I should be learning in the lesson. If itâs critical to solving the problems, shouldnât we learn it prior to the problem?