Skip to content

Instantly share code, notes, and snippets.

@serdaraglamis
Last active June 29, 2017 11:22
Show Gist options
  • Save serdaraglamis/c29eab7886c751f72e0344e683882c9a to your computer and use it in GitHub Desktop.
Save serdaraglamis/c29eab7886c751f72e0344e683882c9a to your computer and use it in GitHub Desktop.
Shuffle Array Items
//ES5 Version
var shuffleArray = function (arr) {
var newArr = arr.slice();
for (var i = newArr.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = newArr[i];
newArr[i] = newArr[j];
newArr[j] = temp;
}
return newArr;
}
// ES6 Version
const shuffleArray = arr => {
const newArr = arr.slice();
for (let i = newArr.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
const temp = newArr[i];
newArr[i] = newArr[j];
newArr[j] = temp;
}
return newArr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment