Math 对象是一个静态工具库,里面装满了数学常数和函数。
Math
常用:Math.round() (四舍五入), Math.floor() (向下取整), Math.ceil() (向上取整)。
Math.round()
Math.floor()
Math.ceil()
我要随机制造混乱!🎲
Math.random() 可以生成 0 到 1 之间的随机小数。
Math.random()
配合取整函数,我们可以做骰子、抽奖等功能!
点击按钮掷骰子 (1-6)。我们需要用 Math.floor(Math.random() * 6) + 1。
Math.floor(Math.random() * 6) + 1