Skip to content

Instantly share code, notes, and snippets.

@rodrigograca31
Created August 12, 2020 11:52
Show Gist options
  • Save rodrigograca31/3401e055198941648908316c77480c93 to your computer and use it in GitHub Desktop.
Save rodrigograca31/3401e055198941648908316c77480c93 to your computer and use it in GitHub Desktop.
Shuffles array in place. ES6 version
/**
* Shuffles array in place. ES6 version
* @param {Array} a items An array containing the items.
* https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
*/
shuffle(a: Array<any>) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment