【发布时间】:2013-12-11 06:00:38
【问题描述】:
我想知道为什么我在控制台收到消息:'Uncaught ReferenceError: d1 is not defined'
我想将掷骰子添加到数组中。
这是我的代码:
<html>
<head>
<script>
var diceRolls = [];
diceRolls.push(d1);
if (diceRolls[diceRolls.length - 1] === d1) {
console.log("Je hebt 5 punten gewonnen!");
}
function rollDice() {
var die1 = document.getElementById("die1");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random()*6) +1;
console.log("You rolled "+d1+".");
}
</script>
</head>
<body>
<div id="die1" class="dice">0</div>
<button onclick="rollDice()"> Roll the dice </button>
<h2 id="status" style="clear:left":> </h2>
</body>
【问题讨论】:
-
...因为您可能从未定义过
d1? -
第一次使用 D1 后定义它。
标签: javascript arrays dice