JS学习记录:
1、js的三种提示框:alert、confirm、prompt
2、随机数取值:
- [0,10)和(0,10]:
向下取整:Math.floor(Math.random()*10) //[0,10)
向上取整:Math.ceil(Math.random()*10) //(0,10]
- [0,10]:
向下取整:Math.floor(Math.random()*11) //[0,10]
向上取整:Math.ceil(Math.random()*11)-1 //[0,10]
- [10,20)和[10,20]:
Math.floor(Math.random()*10)+10 //[10,20)
Math.floor((Math.random()+1)*10) //[10,20)
Math.floor(Math.random()*11)+10 //[10,20]
Math.floor((Math.random()+1)*11) //[11,20]
- [-5,5):
Math.floor(Math.random()*10)-5 //[-5,5)
Math.floor((Math.random()-0.5)*10) //[-5,5)
- [-3,10):
Math.floor(Math.random()*13)-3 //[-3,10)
总结:
`Math.random()*(|max|+|min|)+min //[min,max)
这篇博客介绍了JavaScript中创建提示框的三种方法:alert、confirm和prompt,并详细讲解了如何生成指定范围内的随机数,包括向下和向上取整的技巧。通过实例展示了如何在[0,10),[0,10],[10,20),[-5,5)和[-3,10)等不同区间内生成随机数,总结了一个通用的公式:`Math.random()*(|max|+|min|)+min`。

被折叠的 条评论
为什么被折叠?



