Having a problem with some code

i tried making a random number generator in a function and used an if else statement to get the value and based of of that give an output of “you wind” or “you lose” but i cant gt it to work correctly dose anyone know what i did wrong.

here is the code

function choser() {

return Math.floor(Math.random() * (10 - 1) + 1);

}

if (choser() === 10) {

console.log(“you win!”);

} else {

console.log(“you lose sorry”);

}

  1. Make sure you format your code properly learn how-to here: [How to] Format code in posts

  2. To my knowledge you can’t use a function in a conditional statement so instead save the random number to a variable and use that variable in the if else statement.

1 Like

Math.random() gives you a floating number from 0 to 0.99999…
So the biggest number you can get with Math.random() * 9 is 8.99999…
Then you add 1. That gives you 9.99999…
Then you use floor. That gives you 9 as the highest number you can get.
Therefore choser() === 10 can never be true.
If you want to have an output between 1 and 10, you can use Math.ceil() rather than Math.floor().

thank you it worked i appreciate the help

1 Like