Skip to content

Instantly share code, notes, and snippets.

@arastu
Created September 30, 2018 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arastu/3b55caf019298b791e9453cbbf8f3f96 to your computer and use it in GitHub Desktop.
Save arastu/3b55caf019298b791e9453cbbf8f3f96 to your computer and use it in GitHub Desktop.
Shuffle a JavaScript array
Array.prototype.shuffle = function () {
var copy = this.concat()
var currentIndex = copy.length
while (currentIndex !== 0) {
let randomIndex = Math.floor(currentIndex * Math.random())
currentIndex--
let temp = copy[currentIndex]
copy[currentIndex] = copy[randomIndex]
copy[randomIndex] = temp
}
return copy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment