想知道为什么我的代码不生成数字零,每个数字都是从 1-9 随机生成的。
我需要更改什么以包括 0-9 范围。
function GenerateCaptcha() {
var chr1=Math.ceil(Math.random() * 9)+ '';
非常感谢
最佳答案
来自MDN doc
The Math.ceil() function always rounds a number up to the next largest whole number or integer.
这意味着,如果您得到一个介于 0 和 1 之间的数字,它将四舍五入为 1。
您可以保留原样,或使用 Math.floor
相反。
或者更好,Math.round
, 作为 Levi Johansen建议。这将舍入到最接近的整数。
关于javascript - 为什么此代码不生成数字 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59157320/