I'm creating a dice bot for Discord, and I've got a parameter that is supposed to remove any dices that have rolled a '1' from the requested dice array, so that they can be re-rolled until such times that there are no more 1's.
For example, if the user rolled this dice sequence..
4 5 3 2 1 1
The code would identify that there are two 1's rolled, remove them from the array, and try and re-roll them until they turn up, say '2' and '5' instead, then add them back to the array.
I'm struggling to make my loop function effectively to process the re-rolling process. Here's what I've done so far:
// Reroller
while (rerolls >= 1) {
rerolled.push(Math.floor(Math.random() * diceType + 1));
var latestReroll = rerolled.slice(rerolled.length - 1);
console.log("Latest reroll:" + latestReroll);
// check if the lastest reroll was a 1. If so, delete it and increase the reroll counter accordingly.
if (latestReroll = 1) {
console.log("Rerolling the latest roll.");
rerolled.pop()
console.log(rerolled.push(Math.floor(Math.random() * diceType + 1)));
console.log("New reroll:" + latestReroll);
}
rerolls--;
}
The code tracks the amount of rerolls required by counting the 1's in the previous array (this part works fine and isn't included in the code sample). It's supposed to start creating a new array that could eventually be added to the main dicepool, start generating new numbers, and checking if the newest number generated is a 1 (re-rolling if so). At the moment, it doesn't work. The rerolled array still contains 1s.
I've exhausted my own newbie attempts to try and fix it thus far, so I'd greatly appreciate some help!