Skip to content

Instantly share code, notes, and snippets.

@andreis

andreis/shuffle.js

Created Aug 25, 2016
Embed
What would you like to do?
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