Last active
January 2, 2017 13:29
-
-
Save MD4/b47303440789e530b2f1a33db5e0da93 to your computer and use it in GitHub Desktop.
Adds a shuffle method in array prototype.
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
Array.prototype.shuffle = function() { | |
return this | |
.reduce( | |
([result, rest]) => { | |
const random = (Math.random() * rest.length) >> 0; | |
return [ | |
result | |
.concat(rest[random]), | |
rest | |
.slice(0, random) | |
.concat(rest.slice(random + 1)) | |
]; | |
}, | |
[[], this.slice()] | |
)[0] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment