Created
September 19, 2016 17:48
-
-
Save viebel/5cc67a97903f04036b569c0eb0436e5f to your computer and use it in GitHub Desktop.
This file contains 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
// 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