var a = 0;
var d = 0;
var text = "";
function rollDice() {
var attackScore = document.getElementById("attackThing").value;
document.getElementById("attackScore").innerHTML = attackScore;
var defendScore = document.getElementById("defendThing").value;
document.getElementById("defendScore").innerHTML = defendScore;
//loop
while (attackScore !== 0 && attackScore >= 1 && defendScore >= 1 && defendScore !== 0) {
var al = 0;
var dl = 0;
if (attackScore >= 3) {
a = 3;
} else if (attackScore === 2) {
a = 2;
} else if (attackScore === 1) {
a = 1;}
if (defendScore >= 2) {
d = 2;
} else if (defendScore === 1) {
d = 1;
}
//Rolling the dice
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 d5 = Math.floor(Math.random() * 6) + 1;
//Sorting attacking dice into highest and second highest
if (a === 3) {
var attackDice = [d1, d2, d3];
var da = attackDice.sort();
//i have changed slice value from 1 to 0
var ds = attackDice.slice (0);
var highaResult = Math.max(da[1], da[2]);
var lowaResult = Math.min(da[1], da[2]);
} else if (a === 2) {
attackDice = [d1, d2];
highaResult = Math.max(attackDice[0], attackDice[1]);
lowaResult = Math.min(attackDice[0], attackDice[1]);
} else if (a === 1) {
highaResult = d1;
lowaResult = 0;
}
//sorting defending dice into highest and lowest
if (d === 2) {
var defendDice = [d4, d5];
var highdResult = Math.max(defendDice[0], defendDice[1]);
var lowdResult = Math.min(defendDice[0], defendDice[1]);
} else if (d === 1) {
highdResult = d4;
lowdResult = 0;
}
//determining the winner!
if (highaResult > highdResult) {
defendScore -= 1;
dl += 1;
} else if (highaResult < highdResult) {
attackScore -= 1;
al += 1;
} else if (highaResult === highdResult) {
attackScore -=1 ;
al += 1;
}
if (a > 1 && d > 1) {
if (lowaResult > lowdResult) {
defendScore -= 1;
dl += 1;
} else if (lowaResult < lowdResult) {
attackScore -= 1;
al += 1;
} else if (lowaResult === lowdResult) {
attackScore -= 1;
al += 1;
} }
//displaying result of each roll to user
text += "
Highest Attack: " + highaResult + " Highest Defence: " + highdResult + " Second Highest Attack: " + lowaResult + " Second Highest Defence: " + lowdResult;
text += "