1

I feel I should preface this by saying I am just now really learning how to incorporate anything into JavaScript, so please explain things and answers at a rudimentary level.

I am trying to figure out how to make this script NOT REPEAT an option until ALL options have been exhausted at least once. This is a common problem with Math.random from what I see, but I have no idea how to incorporate a fix to make it not repeat.

var roulette = document.getElementById("rouletteButton");
var stratnames_t = {
  '0': "Strategy 1 Name Here",
  '1': "Strategy 2 Name Here",
};
var objectives_t = {
  '0': "Objective 1 Description Here",
  '1': "Objective 2 Description Here",
};

function randomRoulette() {

  document.getElementById("roulette").style.display = "";

  var rand = Math.floor(2 * Math.random());
  document.getElementsByTagName("objective")[0].innerHTML = objectives_t[rand];
  document.getElementsByTagName("stratname")[0].innerHTML = stratnames_t[rand];

};

function main() {
  roulette.addEventListener("click", function() {
    randomRoulette();
  });
}

main();
j08691
  • 204,283
  • 31
  • 260
  • 272
Kasey Y.
  • 11
  • 2
  • 2
    Put all the options into an array, randomly shuffle the array, then just iterate through the array? – David Apr 12 '22 at 18:03
  • Yes, shuffling is the only sensible way forward here! – Carsten Massmann Apr 12 '22 at 18:11
  • How exactly would I go about doing this? I really have zero basis in scripting - what I have was deconstructed from another script someone wrote so I am 100% dumb here. Thank you for the help! – Kasey Y. Apr 12 '22 at 18:13
  • 1
    Would [Generate unique number within range (0 - X), keeping a history to prevent duplicates](https://stackoverflow.com/q/11808804/1048572) solve your problem? – Bergi Apr 12 '22 at 18:20
  • 1
    Read [this](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – fnostro Apr 12 '22 at 18:24

0 Answers0