How that maths expression works in the return statement?

Tell us what’s happening:

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(5, 10))
console.log(randomRange(5, 10))
console.log(randomRange(5, 10))
console.log(randomRange(5, 10))
console.log(randomRange(5, 10))
console.log(randomRange(5, 10))

Your browser information:

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

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

HI @viveksuman393!

Welcome to the forum!

You have added some extra stuff to the solution.

and

If you delete those extra ** then the test should pass.

I am also going to link the mdn resources for Math.random() and Math.floor() which do a good job of explaining how it works.


Read up on what the two functions (Math.floor and Math.random) do, then try replacing the variables with numbers (on paper, write it down, don’t guess).

See what plugging different numbers in does.

1 Like