Created
January 21, 2019 10:42
-
-
Save userkang/c7c6553a82e803156f0a22fbbee7ae11 to your computer and use it in GitHub Desktop.
打乱数组
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
// ES5版本 | |
function shuffle(a) { | |
var i,j,t | |
for(i = a.length; i; i--) { | |
j = Math.floor(Math.random() * i) | |
t = a[i-1] | |
a[i-1] = a[j] | |
a[j] = t | |
} | |
return a | |
} | |
// ES6版本 | |
function shuffle(a) { | |
for(let i = a.length; i; i--) { | |
let j = Math.floor(Math.random() * i); | |
[a[i-1], a[j]] = [a[j], a[i-1]] | |
} | |
return a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment