2017-08-10 24 views
0

plnkr.co上沒有錯誤,但div按鈕仍然不起作用。使用兩個數組生成「點」的JavaScript胡扯遊戲

1)我在正確的軌道上從兩個數組中拉出兩個隨機數,添加它們並在點擊按鈕時返回結果。 2)我在這裏運行代碼時看到了未定義的錯誤,但我不確定要排除什麼問題。該代碼在plnkr.co上沒有提供任何錯誤。

//button event will generate 2 random numbers and add them together 
 

 
var num1 = [1,2,3,4,5,6]; 
 
var num2 = [1,2,3,4,5,6]; 
 
//var sum1 = [2,3,4,5,6,7,8,9,10,11,12] 
 
//var sum2 = num1 + num2 
 

 
function rollDice(){ 
 
\t var \t randomNumber = Math.floor(Math.random()*(num1 + num2)); 
 
\t document.getElementById("roll").innerHTML = randomNumber[rollDice]; 
 
\t }
body { 
 
    background-color: darkred; 
 
} 
 

 
h1 { 
 
    color: white; 
 
    text-align: center; 
 
} 
 

 
p { 
 
    color: white; 
 
    font-family: verdana; 
 
    font-size: 20px; 
 
}
<!DOCTYPE html> 
 
<html lang="en-US"> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>JavaScript Craps using arrays</title> 
 
\t <link rel="stylesheet" href="random.css"> 
 
\t <script src="craps.js"></script> 
 
\t 
 
</head> 
 
<body> 
 
<h1> Craps Lounge</h1> 
 

 
<div id="button"> 
 
<button onclick="randomNumber()">Roll the Bones</button> 
 
</div> 
 

 
<p id="roll"></p> 
 

 
</body> 
 
</html>

回答

0

與您的代碼的幾個問題:

  • 您需要實際調用rollDice()函數。

  • 你不能找到這樣的randomNumber,你需要兩個隨機索引從你定義的兩個數組中選擇兩個數字。您可以使用Math.random()進行此操作,如下面的代碼所示:

  • randomNumber[rollDice]由於randomNumber是數字而不是對象,因此是錯誤的。

//button event will generate 2 random numbers and add them together 
 

 
var num1 = [1,2,3,4,5,6]; 
 
var num2 = [1,2,3,4,5,6]; 
 
//var sum1 = [2,3,4,5,6,7,8,9,10,11,12] 
 
//var sum2 = num1 + num2 
 

 
function rollDice(){ 
 
    var randomIndex1 = Math.floor(Math.random()*num1.length); 
 
    var randomIndex2 = Math.floor(Math.random()*num2.length); 
 
\t var \t randomNumber = num1[randomIndex1] + num2[randomIndex2]; 
 
\t document.getElementById("roll").innerHTML = randomNumber; 
 
\t }
body { 
 
    background-color: darkred; 
 
} 
 

 
h1 { 
 
    color: white; 
 
    text-align: center; 
 
} 
 

 
p { 
 
    color: white; 
 
    font-family: verdana; 
 
    font-size: 20px; 
 
}
<!DOCTYPE html> 
 
<html lang="en-US"> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>JavaScript Craps using arrays</title> 
 
\t <link rel="stylesheet" href="random.css"> 
 
\t <script src="craps.js"></script> 
 
\t 
 
</head> 
 
<body> 
 
<h1> Craps Lounge</h1> 
 

 
<div id="button"> 
 
<button onclick="rollDice()">Roll the Bones</button> 
 
</div> 
 

 
<p id="roll"></p> 
 

 
</body> 
 
</html>

+0

上一頁代碼: VAR \t randomNumber = Math.floor(的Math.random()*(+ NUM1 NUM2)); 我正在嘗試構建一個將隨機生成的數字舍入的變量。我的推測是該函數會添加並索引兩個單獨的數組然後添加它們。 – liquidsolomon

+0

新代碼: var randomIndex1 = Math.floor(Math.random()* num1.length); var randomIndex2 = Math.floor(Math.random()* num2.length); var \t randomNumber = num1 [randomIndex1] + num2 [randomIndex2]; 在同一個函數內分別索引每個數組,並使用length屬性來引用數組中的項數。另一個變量是爲了將這兩個結果加在一起而創建的 – liquidsolomon

+0

上一頁HTML <按鈕的onclick = 「randomNumber()」>滾動骨骼 新的HTML <按鈕的onclick = 「rollDice()」>滾動骨骼 上一頁HTML被調用的變量。新的HTML正在調用一個函數(對象)。 非常感謝您花時間! – liquidsolomon

0

我看到了一些東西。我重構了我相信你試圖達到的目標。

//button event will generate 2 random numbers and add them together 
 

 
var num1 = [1, 2, 3, 4, 5, 6]; 
 
var num2 = [1, 2, 3, 4, 5, 6]; 
 
//var sum1 = [2,3,4,5,6,7,8,9,10,11,12] 
 
//var sum2 = num1 + num2 
 

 
function rollDice() { 
 
    
 
    //random # between 1 and 6 
 
    var dice1 = Math.floor((Math.random() * num1.length) + 1); 
 
    var dice2 = Math.floor((Math.random() * num2.length) + 1); 
 
    
 
    var randomNumber = dice1 + dice2; 
 
    document.getElementById("roll").innerHTML = randomNumber; 
 
}
body { 
 
    background-color: darkred; 
 
} 
 

 
h1 { 
 
    color: white; 
 
    text-align: center; 
 
} 
 

 
p { 
 
    color: white; 
 
    font-family: verdana; 
 
    font-size: 20px; 
 
}
<!DOCTYPE html> 
 
<html lang="en-US"> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>JavaScript Craps using arrays</title> 
 
\t 
 
</head> 
 
<body> 
 
<h1> Craps Lounge</h1> 
 

 
<div id="button"> 
 
<button onclick="rollDice()">Roll the Bones</button> 
 
</div> 
 

 
<p id="roll"></p> 
 

 
</body> 
 
</html>

+0

我成功實現了第一個答案。我發現你的陳述包含+1並返回相同的結果。因此是包含+1外來的? – liquidsolomon

+0

我相信它只是標誌着工作的答案。人們可以採取多種途徑解決問題。所以可以有多個正確答案。 –

+0

非常感謝! – liquidsolomon