Skip to content

Instantly share code, notes, and snippets.

@blazsmaster
Created August 23, 2022 10:07
Show Gist options
  • Save blazsmaster/a7e45af84d626959bbec6461f46cf5de to your computer and use it in GitHub Desktop.
Save blazsmaster/a7e45af84d626959bbec6461f46cf5de to your computer and use it in GitHub Desktop.
Simple way to shuffle an array
/**
* @param {Array} array
* @returns {Array}
*/
function shuffleArray(array) {
let currentIndex: number = array.length;
let randomIndex: number;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment