Created
December 21, 2015 11:13
-
-
Save bendc/5b001e40754022125172 to your computer and use it in GitHub Desktop.
Randomize arrays
This file contains 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
const shuffle = (arr, mixed = [], pool = arr.slice()) => { | |
const remaining = pool.length; | |
if (!remaining) return mixed; | |
const index = Math.floor(Math.random() * remaining); | |
const el = pool.splice(index, 1).pop(); | |
mixed.push(el); | |
return shuffle(arr, mixed, pool); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: