如何在一个 js 函数中执行两个函数,从而将两组随机数放入一个 html 输入中?我想创建一键生成两组随机数的函数。
生成 1-50 中的 5 个数字,然后生成 1-10 中的 2 个数字。一行中两组数字。
此代码不起作用:
function eurojackpot() {
let result = new Set();
while (result.size != 5) {
result.add(Math.floor(Math.random() * 50) + 1);
}
document.getElementById("eurojackpot").innerText = [...result];
let result = new Set();
while (result.size != 2) {
result.add(Math.floor(Math.random() * 10) + 1);
document.getElementById("eurojackpot2").innerText = [...result];
}
}
eurojackpot();
<div id="eurojackpot"></div>
<div id="eurojackpot2"></div>
我想要实现的目标:
exp - [2, 5, 15, 23, 49] + [1, 10]
最佳答案
当您删除结果的第二个“let”时,代码将起作用:
function eurojackpot() {
let result = new Set();
while(result.size != 5){
result.add(Math.floor(Math.random()*50)+1);
}
document.getElementById("eurojackpot").innerText = [...result];
result = new Set();
while(result.size != 2){
result.add(Math.floor(Math.random()*10)+1);
}
document.getElementById("eurojackpot2").innerText = [...result];
}
这是一个重现它的 JSFiddle:
关于javascript - 如何在一个html输入中生成两组随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49978479/