Generate Random Whole Numbers within a Range - solution not passing tests

Hi, I can’t understand why my solution doesn’t pass this test: " randomRange should use both myMax and myMin , and return a random number in your range."

Here is what I wrote and it seems to work just fine:

function randomRange(myMin, myMax) {
  // Only change code below this line
  return Math.round(Math.random() * (myMax - myMin) + myMin);
  // Only change code above this line


Try using the formula given to you in the exercise (it involves adding one to the difference of myMax and myMin).

1 Like

The MDN page on Math.random() also has a few examples and some explanations.

I remember seeing this thread as well at one point (didn’t read it again but I think it gives a good explanation)

1 Like

I found the reason already, unfortunately it doesn’t accept solution with Math.round and we have to use Math.floor with that formula @hbar1st wrote about.

Thanks for your support anyways and sorry for not clarifying that I don’t need help anymore…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.