Created
August 31, 2015 12:28
-
-
Save laispace/59179ca4f1644cca856f to your computer and use it in GitHub Desktop.
打乱数组顺序
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
function randomArray (array) { | |
if (array.length === 1) { | |
return array; | |
} else { | |
var len = array.length; | |
var index = ~~(Math.random() * len); | |
var item = array.splice(index, 1); | |
return item.concat(randomArray(array)); | |
} | |
} | |
var array = [1,2,3,4,5,6]; | |
var result = randomArray(array); | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment