Created
August 10, 2019 10:44
-
-
Save karpolan/d258520d7417353c3f1b39aeb2b9b812 to your computer and use it in GitHub Desktop.
Shuffle the Array in JavaScript
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
/* eslint-disable import/prefer-default-export */ | |
/* eslint-disable no-param-reassign */ | |
/** | |
* Shuffles array in place. ES6 version. | |
* @param {Array} a - array containing all items to shuffle. | |
*/ | |
export function shuffle(a) { | |
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