title: How to Randomly Choose Selected Item in Select Dropdown that wasn't chosen before in JavaScript?
body: <p>On a button click, I want to run a function that will each time randomly select an option from a select dropdown that wasn't chosen before. And if all options were chosen, I want to reset the used options and start from the beginning.</p>
  
  <p>I have a function that chooses an element that wasn't chosen before randomly from an array (<a href="https://stackoverflow.com/questions/60812026/how-to-randomly-choose-an-element-from-an-array-that-wasnt-chosen-before-in-jav?noredirect=1#comment107592660_60812026">credit</a> to @Rajesh)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function randomize(arr) {
    let data = [...arr];
    let chosenItems = [];
  
    function getRandomValue() {
      if (data.length === 0) {
        data = chosenItems;
        chosenItems = [];
      }
      const index = Math.floor(Math.random() * data.length);
      const choice = data.splice(index, 1)[0];
  
      chosenItems.push(choice);
      return choice;
    }
    
    return {
      randomItem: getRandomValue
    }
  }
  
  const dummyData = [ 1,2,3,4,5 ];
  
  const randomizeData = randomize(dummyData);
  
  for (let i = 0; i&lt; 10; i++) {
    console.log(randomizeData.randomItem())
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have already created a script that randomly chooses an element from a select2 dropdown but it selects the same element very often.</p>
  
  <pre class="lang-js prettyprint-override"><code>var optionsArray = new Array();
  var first = $('select option')[0];
  $('select option').each(function () { optionsArray.push(this) });
  
  if (optionsArray.length &gt; 1) {
      $('select').html('');
  
      var i = 0;
  
      var random
  
      while (i &lt; optionsArray.length) {
  
          random = Math.floor(Math.random() * optionsArray.length)
          //if element hasn't been marked as "selected"
          if (optionsArray[random] != "selected") {
              $("select").append(optionsArray[random]);
              //mark element as selected
              optionsArray[random] = "selected"
              i++
          }
       }
  
      var newSelectedOption = $('select option')[0];
      $('select').val($(newSelectedOption).val()).trigger('change');
  </code></pre>