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>
上一頁代碼: VAR \t randomNumber = Math.floor(的Math.random()*(+ NUM1 NUM2)); 我正在嘗試構建一個將隨機生成的數字舍入的變量。我的推測是該函數會添加並索引兩個單獨的數組然後添加它們。 – liquidsolomon
新代碼: 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
上一頁HTML <按鈕的onclick = 「randomNumber()」>滾動骨骼 新的HTML <按鈕的onclick = 「rollDice()」>滾動骨骼 上一頁HTML被調用的變量。新的HTML正在調用一個函數(對象)。 非常感謝您花時間! – liquidsolomon