Last active
August 29, 2015 14:02
-
-
Save federicobucchi/780f0b94ceb0d7bc4a0e to your computer and use it in GitHub Desktop.
Array Shuffle
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
var arr = [ 1 , 2 , 3 , 4 , 5 ]; | |
function shuffle( arr ) { | |
var inputArr = arr; | |
var shuffledArr = []; | |
var loops = inputArr.length; | |
var randomNumber; | |
for ( var i = 0; i < loops; i++ ) { | |
randomNumber = Math.floor( Math.random() * ( loops - i ) ); | |
shuffledArr.push( inputArr[ randomNumber ] ); | |
inputArr.splice( randomNumber , 1 ); | |
} | |
return shuffledArr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment