Javascript Array getRandomArrayItem()

Description

Javascript Array getRandomArrayItem()


function shuffleArray(array) {
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];/*  w w  w. j av  a 2s . com*/
    array[j] = temp;
  }
  return array;
}

Array.prototype.getRandomArrayItem = function() {
  return this[Math.floor(Math.random()*this.length)];
}



PreviousNext

Related