Skip to content

Instantly share code, notes, and snippets.

@Jberivera
Created January 30, 2019 19:10
Show Gist options
  • Save Jberivera/7fc35beb750e886df8da7cc42fca8089 to your computer and use it in GitHub Desktop.
Save Jberivera/7fc35beb750e886df8da7cc42fca8089 to your computer and use it in GitHub Desktop.
function shuffle(array, result = []) {
if (array.length === 0) {
return result;
}
const randomIndex = Math.floor(Math.random() * (array.length - 1));
const item = array[randomIndex];
const tail = [
...array.slice(0, randomIndex),
...array.slice(randomIndex + 1),
];
return shuffle(tail, [...result, item]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment