generate a random number between 1 and 10 in javascript

To generate a random number between 1 and 10 in JavaScript, you can use the Math.random() function. Since Math.random() returns a random number between 0 and 1, we can multiply the result by 10, add 1 and then use the Math.floor() function to get an integer value between 1 and 10.

Here's the code:

index.tsx
const randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber); // Outputs a random number between 1 and 10
128 chars
3 lines

In the above code, Math.floor() is used to round the result down to the nearest integer, so if Math.random() returns 0.5, the resulting random number would be 6.

gistlibby LogSnag