For those who engage in D&D type games, I have created two javascript functions to simulate dice rolls.
The initial function, known as getRandom(), accepts a low number and high number as parameters and then produces a random integer between them.
function getRandom(low, high) {
return Math.floor(Math.random() * (high - low + 1)) + low;
}
The second function, dropLow(), is designed to roll the dice four times, remove the lowest scoring die, and sum up the remaining three. If the total exceeds 9, it will be returned.
function dropLow() {
var rolls = [],
lowest,
total;
do {
total = 0;
for (x = 0; x < 4; x++) {
rolls.push(getRandom(1,6));
total += rolls[x];
}
lowest = rolls[0];
for (x = 1; x < 4; x++) {
if (rolls[x] < lowest) {
lowest = rolls[x];
}
}
total -= lowest;
} while (total < 10);
rolls.length = 0;
return total;
}
While calling dropLow() once seems to work fine initially, multiple calls start causing my browser (Chrome) to crash with the error message: "Aw Snap! Something went wrong while displaying this webpage." The issue persists even when trying different browsers or devices.
I've explored options like destroying or clearing out the array's contents to prevent continuous building on the original data, but the problem remains unresolved.
If anyone has insights on why or how this is happening, I would greatly appreciate your help!