【发布时间】:2021-01-15 15:46:45
【问题描述】:
当我“单击”我的按钮#roll 时,我希望有一个不同的随机数,但我总是得到相同的数字。有人可以帮帮我吗?
这是我的代码:
//création nombre aléatoire
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max, min + 1)+ min);
}
numberRandom = getRandomInt(1, 7)
//création du lancé de dé
const roll = document.getElementById("roll")
roll.addEventListener('click',()=>{
alert(numberRandom)
})
<button class="favorite styled" type="button" id="roll">
Lancer le dé
</button>
【问题讨论】:
-
这看起来像是 this 的副本。它没有解释为什么你的代码坏了,但我猜你宁愿拥有正确的代码而不是解释。
-
Math.floor(Math.random() * (max, min + 1)+ min);没有意义。你的意思是Math.floor(Math.random() * (max - min + 1)+ min);?
标签: javascript random