Created
March 9, 2017 03:42
-
-
Save akirattii/d71312789c8650b434942c3c5c067ff7 to your computer and use it in GitHub Desktop.
Shuffles array using pure js
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 | |
| * @param {Array} - array | |
| */ | |
| function shuffle(a) { | |
| let len = a.length; | |
| for (let i = len; i; i--) { | |
| let j = Math.floor(Math.random() * i); | |
| [a[i - 1], a[j]] = [a[j], a[i - 1]]; | |
| } | |
| } | |
| let arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9']; | |
| shuffle(arr); | |
| console.log(arr); | |
| /* | |
| Console log: | |
| ["7", "2", "8", "1", "5", "3", "9", "4", "6"] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment