Javascript 骰子掷成一个数组

Javascript 骰子掷成一个数组,javascript,arrays,dice,Javascript,Arrays,Dice,我想知道为什么我会在控制台上收到这样的消息:“uncaughtreferenceerror:d1未定义” 我想把骰子卷加入一个数组 这是我的代码: <html> <head> <script> var diceRolls = []; diceRolls.push(d1); if (diceRolls[diceRolls.length - 1] === d1) { console.log("Je hebt 5 punten ge

我想知道为什么我会在控制台上收到这样的消息:“uncaughtreferenceerror:d1未定义”

我想把骰子卷加入一个数组

这是我的代码:

<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>

var=[];
推骰子(d1);
if(骰子滚[骰子滚.长度-1]==d1){
控制台日志(“Je hebt 5 punten gewonnen!”);
}     
函数rollDice(){
var die1=document.getElementById(“die1”);
var status=document.getElementById(“状态”);
var d1=数学地板(数学随机()*6)+1;
log(“您滚动了“+d1+”);
}
0
掷骰子

脚本的第二行试图在您尚未初始化
d1
时将
d1
推入
dicerlls
。您必须在脚本其余部分的上方声明
rollDice()
,然后调用它将值推入
diceRolls

我想你可能想试试这些东西

var diceRolls = [];

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+".");
  diceRolls.push(d1);
}

if (diceRolls[diceRolls.length - 1] === d1) {
    console.log("Je hebt 5 punten gewonnen!");
}     

脚本的第二行试图在您尚未初始化
d1
时将
d1
推入
dicerlls
。您必须在脚本其余部分的上方声明
rollDice()
,然后调用它将值推入
diceRolls

我想你可能想试试这些东西

var diceRolls = [];

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+".");
  diceRolls.push(d1);
}

if (diceRolls[diceRolls.length - 1] === d1) {
    console.log("Je hebt 5 punten gewonnen!");
}     

脚本的第二行试图在您尚未初始化
d1
时将
d1
推入
dicerlls
。您必须在脚本其余部分的上方声明
rollDice()
,然后调用它将值推入
diceRolls

我想你可能想试试这些东西

var diceRolls = [];

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+".");
  diceRolls.push(d1);
}

if (diceRolls[diceRolls.length - 1] === d1) {
    console.log("Je hebt 5 punten gewonnen!");
}     

脚本的第二行试图在您尚未初始化
d1
时将
d1
推入
dicerlls
。您必须在脚本其余部分的上方声明
rollDice()
,然后调用它将值推入
diceRolls

我想你可能想试试这些东西

var diceRolls = [];

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+".");
  diceRolls.push(d1);
}

if (diceRolls[diceRolls.length - 1] === d1) {
    console.log("Je hebt 5 punten gewonnen!");
}     
浏览器到达这一行时,d1尚未设置。我想如果你把它放到rollDice函数中,你就会得到你想要的东西。换言之:

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+".");
       diceRolls.push(d1);
 }
编辑:AVP以30秒的优势击败了我,他得到了我的选票

浏览器到达这一行时,d1尚未设置。我想如果你把它放到rollDice函数中,你就会得到你想要的东西。换言之:

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+".");
       diceRolls.push(d1);
 }
编辑:AVP以30秒的优势击败了我,他得到了我的选票

浏览器到达这一行时,d1尚未设置。我想如果你把它放到rollDice函数中,你就会得到你想要的东西。换言之:

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+".");
       diceRolls.push(d1);
 }
编辑:AVP以30秒的优势击败了我,他得到了我的选票

浏览器到达这一行时,d1尚未设置。我想如果你把它放到rollDice函数中,你就会得到你想要的东西。换言之:

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+".");
       diceRolls.push(d1);
 }

编辑:AVP以30秒的优势击败了我,他得到了我的选票。

…因为你从未定义过
d1
也许?你在第一次使用它之后定义了d1…因为你从未定义过
d1
也许?你在第一次使用它之后定义了d1…因为你从未定义过
d1
也许?你在第一次使用它之后定义了d1时间……因为您从未定义过d1,也许?您是在第一次使用d1之后定义的。