Answers for "array random access"

0

define function to get random value from array

const randomValue = (list) => {
    return list[Math.floor(Math.random() * list.length)];
};
Posted by: Guest on January-30-2022
0

Access the Full Array random

Array.prototype.shuffle = function(){
  var i = this.length,
      j,
    tmp;
  while (i > 1) {
    j = Math.floor(Math.random()*i--);
    tmp = this[i];
    this[i] = this[j];
    this[j] = tmp;
  }
  return this;
};

var arr = [1,2,3,4,5].shuffle();
for(var i = 0; i < arr.length; i++) console.log(arr[i]);
Posted by: Guest on January-14-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language