Skip to content

Instantly share code, notes, and snippets.

@andreis
Created August 25, 2016 11:32
Show Gist options
  • Save andreis/d4a76f2aa60293509ac5c44370ab19dc to your computer and use it in GitHub Desktop.
Save andreis/d4a76f2aa60293509ac5c44370ab19dc to your computer and use it in GitHub Desktop.
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