Skip to content

Instantly share code, notes, and snippets.

@mohamedhayibor
Created December 27, 2015 16:36
Show Gist options
  • Save mohamedhayibor/9609c3cbc3618e97d6c3 to your computer and use it in GitHub Desktop.
Save mohamedhayibor/9609c3cbc3618e97d6c3 to your computer and use it in GitHub Desktop.
var arr = [1, 2, 4, 7, 9, 6, 3];
function flatten() {
return [].reduce.call(arguments, function(sum, val) {
return Array.isArray(val) ? sum.concat(flatten.apply(null, val)) : sum.concat(val);
}, []);
}
function shuffle (array) {
var temp = [];
while (0 !== array.length) {
temp.push(array.splice(Math.floor(Math.random() * (array.length -1)), 1 ) );
}
return flatten(temp);
}
console.log(shuffle(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment