I am currently developing a theoretically basic generator of sorts with the goal that the generator will have a finite amount of possible answers in an array, and that the possible answers will not be repeated in a shuffle until ALL have been exhausted. I have managed to create a shuffled array that prevents the same option being chosen twice consecutively, but I am still getting repeats before all options are used. I hope this makes sense.
For example -
I have the options of [1, 2, 3, 4, 5] in my array.
I click the "Give me an option" button 7 times to get 7 outputs.
My ideal results would be:
CORRECT: [1, 5, 3, 2, 4, 5, 2] - in which all 5 options are used once before repeating
INCORRECT: [1, 5, 2, 5, 4, 2, 1] - in which #5 was repeated before all options were used
My current working script is as follows:
var roulette = document.getElementById("rouletteButton");
var strats = {
'0': "<h1>Strat Title 1</h1> Info here",
'1': "<h1>Strat Title 2</h1> Description here.",
'2': "<h1>Strat Title 3</h1> Stuff here.",
'3': "<h1>Strat Title 4</h1> Nonsense here.",
'4': "<h1>Strat Title 5</h1> Lorem Ipsum here.",
'5': "<h1>Strat Title 6</h1> Donuts here.",
};
var previousRandom = 0;
function randomStrat() {
document.getElementById("roulette").style.display = "";
var randomIndex = Math.floor(Math.random() * 6);
do {
randomIndex = Math.floor(Math.random() * 6);
if(randomIndex !== previousRandom) {
shuffle(randomIndex);
}
}
while (randomIndex == previousRandom) {
previousRandom = randomIndex;
}
}
function shuffle(randomIndex) {
document.getElementsByTagName("results")[0].innerHTML = strats[randomIndex];
}
function main() {
roulette.addEventListener("click", function() {
randomStrat();
});
}
main();
How exactly would I go about creating a history record, and assuring strats options are fully exhausted before they can be repeated on click? Thanks!