Created
August 12, 2020 11:52
-
-
Save rodrigograca31/3401e055198941648908316c77480c93 to your computer and use it in GitHub Desktop.
Shuffles array in place. ES6 version
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
/** | |
* Shuffles array in place. ES6 version | |
* @param {Array} a items An array containing the items. | |
* https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array | |
*/ | |
shuffle(a: Array<any>) { | |
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