以下代码完美适用于我正在做的事情,但我想知道是否有方法使用数组截断它。简化JavaScript变量
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;
}
为什么不一个函数*返回*值。然后,您可以将其用于所有不同的属性。 – Pointy
感谢您的及时回复。有没有关于返回价值的更多细节? – amfilipiak
@amfilipiak我已为您更新的问题提供了更新的答案。 –