Created
January 30, 2019 19:10
-
-
Save Jberivera/7fc35beb750e886df8da7cc42fca8089 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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