Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Many thanks to http://stackoverflow.com/questions/9960908/permutations-in-javascript/9960925#9960925
permutations = function(arr){
var permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
};
return permute(arr);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment