Skip to content

Instantly share code, notes, and snippets.

@sayhicoelho
Created July 17, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sayhicoelho/381d15df6bff074e4c6d8e27d83ee8a8 to your computer and use it in GitHub Desktop.
Save sayhicoelho/381d15df6bff074e4c6d8e27d83ee8a8 to your computer and use it in GitHub Desktop.
JavaScript: Shuffle Array
function shuffleArray(array) {
let currentIndex = array.length
let temporaryValue, randomIndex
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment