Challenge Generate Random Whole Numbers within a Range has an issue.
User Agent is: Mozilla/5.0 (Windows NT 5.1; rv:43.0) Gecko/20100101 Firefox/43.0
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
// Example
function ourFunction(ourMin, ourMax) {
return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
}
ourFunction(1, 9);
// Only change code below this line.
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin)) + myMin; // Change this line
}
// Change these values to test your function
var myRandom = randomRange(5, 15);
Checked the maxima (Math.random()=1) and minima(Math.random()=0) and and the solution is right but the target "The random number generated by randomRange should be less than or equal to your maximum number, myMax. " won't evaluate to true.
randomRange(5, 15)
in the code above has a maximum possible value of 14 instead of 15 (myMax
).
I think the first two test messages should be reworded so that it's clear that the maximum value in the range should equal myMax
and the minimum value should equal myMin
.
@eratio08 To expand on what @alistermada wrote, the maximum of Math.random()
is not actually 1. The range is [0,1)
, which is inclusive of 0 but exclusive of 1. So the real maximum is something like 0.99999..., but when combined with Math.floor()
the maximum is 0 unless the result is multiplied (or increased in some way) first.
Although it probably didn't cause the confusion here, I agree the text should be clarified.
@eratio08 your code doesn't match the code example in the instructions, as you removed the +1
. If you get stuck with a challenge, please ask questions to the Help Room.
@BKinahan I like the change in wording 馃槃
return Math.floor(Math.random() * (myMax - myMin+1)) + myMin; // Change this line you forgot to add the 1 inside.
check this linkhttps://forum.freecodecamp.com/t/challenge-generate-random-whole-numbers-within-a-range/18187
What @BEBEDYDY said! Adding the 1 to the myMax did the trick 馃憤
Most helpful comment
randomRange(5, 15)
in the code above has a maximum possible value of 14 instead of 15 (myMax
).I think the first two test messages should be reworded so that it's clear that the maximum value in the range should equal
myMax
and the minimum value should equalmyMin
.