Created
August 25, 2016 11:32
-
-
Save andreis/d4a76f2aa60293509ac5c44370ab19dc 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
function shuffle(arr) { | |
if (!Array.isArray(arr)) { throw "need an array as input"; } | |
if (arr.length < 2) { return arr; } | |
var pick = Math.floor(Math.random() * (arr.length)); | |
var tmp = arr[pick]; arr[pick] = arr[0]; arr[0] = tmp; | |
return [arr[0]].concat(shuffle(arr.slice(1))); | |
} | |
shuffle([1,2,3,4,5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment