The following code works perfectly for what I am doing but I was wondering if there is a way to truncate it using an array.
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;
}
I have tried putting all the variables into an array but can not for the life of me get the function to fire. Any assistance in being able to truncate this code would be greatly appreciated as there are going to be 7 different blocks all with 4 variables that will be fired at once. I can continue this function as is for the 7 different stats (i.e strengthTotal") however the block would be huge for something that should be very simple.
I dont know if i was really be clear enough with what I am trying to do. I am posting the full HTML, JavaScript, and CSS code so you all can see what I have so far. (when you see the full JScript you will probably either laugh, cry, cringe or all of the above) As i said I have a basic understanding from about 15 years ago and have not really worked with code like this for awhile. This way you can see what I am seeing. So here it goes.
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;
}
and finally - dice.css:
div.dice {
width: 32px;
background: #f5f5f5;
border: #999 1px solid;
padding: 10px;
font-size: 24px;
text-align: center;
margin: 5px;
}