lundi 4 septembre 2017

Unique random item from array

In JavaScript, I would like to randomly remove an item from an array and display this within the HTML on a button click. Then on the next click of the button show the next removed item from the array. However, this doesn't seem to be working!

The fiddle: http://ift.tt/2vFt4W8

document.getElementById("Button").onclick = function() {
  var count = 3;
  var myArray = [
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G'
  ];
  var tmpArrayE = myArray.slice(myArray);
  var goE = [];

  for (var i = 0; i < count; i++) {
    var optionsE = Math.floor(Math.random() * tmpArrayE.length);
    var removedE = tmpArrayE.splice(optionsE, 1);

    goE.push(removedE[0]);
  }
  document.getElementById("Answer").innerHTML = goE[0];
}




Aucun commentaire:

Enregistrer un commentaire