Ask coding questions

← Back to all posts
random number js
aiden765 (5)

so i tried

console.log(Math.random()*10)

but i didnt work it returned a decimal i want a whole number

Answered by badst (240) [earned 5 cycles]
View Answer
Comments
hotnewtop
badst (240)
console.log(Math.floor(Math.random()*10)

use Math.floor to round up the number.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

aiden765 (5)

@pepelaugh k it works thx. i checked the mark thing.

badst (240)

@aiden765 np, just remember that these questions are easily answered if you just look it up online or on stackoverflow.