11.
Which of the following expressions generates a random number between 1 and 10?
Math.ceil(Math.random() * 10)
Math.random() + 10
Math.floor(Math.random() * 10) + 1
Math.random() * 10
Correct: C
To generate a random number between 1 and 10, we can use the Math.random() function together with Math.floor(). Math.floor() rounds a number down to the nearest whole number. By multiplying Math.random() by 10, we get a random float between 0 and 10. By then adding 1, we shift the range to be between 1 and 10. Finally, using Math.floor() ensures that the result is a whole number.
const randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber); // Outputs a random number between 1 and 10