Tell us what’s happening:
Describe your issue in detail here.
I have finished the challenge but I still don’t know why should we have plus > myMin at the end
Your code so far
function randomRange(myMin, myMax) {
// Only change code below this line
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
// Only change code above this line
}
console.log(randomRange(3,8))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Generate Random Whole Numbers within a Range
This part makes sure that the minimum random number will be myMin.
Simple way to see that is to replace the
With 0
(Math.random() can return zero to less than 1 values so when it does, the + myMin; will be very important to make sure the minimum is always equal or above myMin)
After you test with 0, try testing with 0.1 and 0.2 etc in place of the Math.random() call.
This is just some simple maths to get the multiplier that will move and spread the randomly generated number to a higher range.
If I have the number 0.1 returned by Math.random, then how do I make this number bigger respective of the given range?
(If the myMin for eg is 10 and the myMax is 20?)
Ask yourself how would you map the 0.1 to a value inside the 10 to 20 range?