Answers for "javascript randomly reorder array"

11

how to randomly sort an array javascript

array.sort(() => Math.random() - 0.5);
Posted by: Guest on May-29-2021
13

javascript randomly shuffle array

function randomArrayShuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}
var alphabet=["a","b","c","d","e"];
randomArrayShuffle(alphabet); 
//alphabet is now shuffled randomly = ["d", "c", "b", "e", "a"]
Posted by: Guest on July-24-2019

Code answers related to "javascript randomly reorder array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language