Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js how to shuffle array algoritm. The Fisher-Yates algorith

// @ts-check

// The Fisher-Yates algorith

(() => {
  let arr = [1, 2, 3, 4, 5];

  const shuffleArray = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      const temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
  };

  shuffleArray(arr);
  console.log(arr);

  shuffleArray(arr);
  console.log(arr);

  shuffleArray(arr);
  console.log(arr);
})();
 
PREVIOUS NEXT
Tagged: #js #shuffle #array #The #algorith
ADD COMMENT
Topic
Name
8+9 =