我希望我的程序返回 10 个随机数,但它不起作用,它不加载随机数,我已将程序链接到 index.html
所以这不是这里的问题,这是我尝试过的:
function randomNumber(upper) {
return Math.floor(Math.random() * upper) + 1;
}
var counter = 0;
while (counter < 10) {
var randNum = randomNumber(6);
document.write(randNum + ' ');
counter += 1;
}
最佳答案
如果它在 fiddle 中工作,但在其他地方不起作用,则很可能是这里的 onload
问题,正如 Doorknob 已经建议的那样。
function randomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
window.onload = function () {
var counter = 0;
while (counter < 10) {
var randNum = randomNumber(6);
document.write(randNum + ' ');
counter += 1;
}
};
window.onload
中的括号会导致代码等待整个页面加载完毕。
关于javascript - 随机数循环程序 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33671553/