Last active
June 29, 2017 11:22
-
-
Save serdaraglamis/c29eab7886c751f72e0344e683882c9a to your computer and use it in GitHub Desktop.
Shuffle Array Items
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
//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