Fisher-Yates algorith : (Really) shuffle and array

PHOTO EMBED

Sat Jun 19 2021 07:12:38 GMT+0000 (Coordinated Universal Time)

Saved by @hisam #javascript #vanilla #sort #randomize #array

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;
  }
}
content_copyCOPY

Loop through the array (from the end to the start) and pick a random item from the array, swapping it with the item in the current iteration.