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

0

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);
})();
Posted by: Guest on May-06-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language