Skip to content

Instantly share code, notes, and snippets.

@karpolan
Created August 10, 2019 10:44
Show Gist options
  • Save karpolan/d258520d7417353c3f1b39aeb2b9b812 to your computer and use it in GitHub Desktop.
Save karpolan/d258520d7417353c3f1b39aeb2b9b812 to your computer and use it in GitHub Desktop.
Shuffle the Array in JavaScript
/* eslint-disable import/prefer-default-export */
/* eslint-disable no-param-reassign */
/**
* Shuffles array in place. ES6 version.
* @param {Array} a - array containing all items to shuffle.
*/
export function shuffle(a) {
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