Created
August 23, 2022 10:07
-
-
Save blazsmaster/a7e45af84d626959bbec6461f46cf5de to your computer and use it in GitHub Desktop.
Simple way to shuffle an array
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
/** | |
* @param {Array} array | |
* @returns {Array} | |
*/ | |
function shuffleArray(array) { | |
let currentIndex: number = array.length; | |
let randomIndex: number; | |
while (currentIndex !== 0) { | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex--; | |
[array[currentIndex], array[randomIndex]] = [ | |
array[randomIndex], | |
array[currentIndex], | |
]; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment