Basic JavaScript - Generate Random Whole Numbers within a Range

Tell us what’s happening:
Can anyone correct me please?

Your code so far

function randomRange(myMin, myMax) {
  let x= "";
  x= Math.floor(Math.random()*myMax);
 if (x>=myMin){
  return x;
 }
  else  {
   return myMax;
 } 
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Generate Random Whole Numbers within a Range

Link to the challenge:

you already have the formula explained and you are asked to use it directly to return a random whole number within a
given range, return it …
The Math.random() function generates a random decimal number between 0 (inclusive) and 1 (exclusive). By multiplying it with (myMax - myMin + 1), we obtain a random decimal number between 0 (inclusive) and myMax - myMin + 1 (exclusive). Adding myMin to it shifts the range to be between myMin (inclusive) and myMax (inclusive). Finally, Math.floor() rounds down the decimal value to the nearest whole number.Return the formula that was given in the lesson

1 Like

There is no logic involved here. You can literally copy and paste the example code, change the variable names so they match the parameters, and return the result.

Thank you very much for correcting me :heart: :heart:

Thank you very much for correcting me. I got the point :heart: :heart:

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