【发布时间】:2016-06-30 01:50:18
【问题描述】:
以下代码非常适合我正在做的事情,但我想知道是否有办法使用数组截断它。
function rollDice() {
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
var strengthTotal = diceTotal;
document.getElementById("strengthTotal").innerHTML = diceTotal;
}
我尝试将所有变量放入一个数组中,但我终生无法触发该函数。能够截断此代码的任何帮助将不胜感激,因为将有 7 个不同的块,所有块都带有 4 个变量,它们将立即被触发。对于 7 个不同的统计数据(即strengthTotal),我可以继续这个功能,但是对于应该非常简单的东西来说,这个块将是巨大的。
我不知道我是否真的对我正在尝试做的事情足够清楚。我发布了完整的 HTML、JavaScript 和 CSS 代码,所以你们都可以看到我到目前为止的内容。 (当您看到完整的 JScript 时,您可能会大笑、哭泣、畏缩或以上所有内容) 正如我所说的,我从大约 15 年前开始有了基本的了解,并且有一段时间没有真正使用过这样的代码了。这样你就可以看到我所看到的。就这样吧。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="dice.css">
<script src="Script2.js"></script>
</head>
<body>
<h2>Testing Differnet Types of Rolls</h2>
<p> 4D6 Totaling 3 best rolls</p>
<button onclick="rollDice()">Roll Dice</button><br>
<div>
<p>Strength</p>
<div id="strengthTotal" class="dice">0</div>
</div>
<div>
<p>Dexterity</p>
<div id="dexterityTotal" class="dice">0</div>
</div>
<div>
<p>Constitution</p>
<div id="constitutionTotal" class="dice">0</div>
</div>
<div>
<p>Intelligence</p>
<div id="intelligenceTotal" class="dice">0</div>
</div>
<div>
<p>Wisdom</p>
<div id="wisdomTotal" class="dice">0</div>
</div>
<div>
<p>Charisma</p>
<div id="charismaTotal" class="dice">0</div>
</div>
</body>
</html>
JavaScript:
function rollDice() {
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var 3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("strengthTotal").innerHTML = diceTotal;
var 1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("dexterityTotal").innerHTML = diceTotal;
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("constitutionTotal").innerHTML = diceTotal;
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("intelligenceTotal").innerHTML = diceTotal;
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("wisdomTotal").innerHTML = diceTotal;
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var d4 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3 + d4 - Math.min(d1, d2, d3, d4);
document.getElementById("charismaTotal").innerHTML = diceTotal;
}
最后 - dice.css:
div.dice {
width: 32px;
background: #f5f5f5;
border: #999 1px solid;
padding: 10px;
font-size: 24px;
text-align: center;
margin: 5px;
}
【问题讨论】:
-
为什么不创建一个返回值的函数。然后,您可以将其用于所有不同的属性。
-
感谢您的及时回复。您是否可以分享有关返回值的更多详细信息?
-
@amfilipiak 对于您更新的问题,我有一个更新的答案。
-
为什么骰子总数不是骰子总数?
标签: javascript arrays var