Created
October 19, 2016 10:49
-
-
Save ericat/440692fbca32f03b359b9c4e5af11c24 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
'use strict' | |
var results = []; | |
function perm(list, memo) { | |
for(var i=0; i < list.length; i++) { | |
var current = list.splice(i,1)[0]; | |
if(list.length === 0) { | |
var permutation = memo.concat(current); | |
results.push(permutation.join('')); | |
} | |
perm(list.slice(), memo.concat(current)); | |
list.splice(i, 0, current); | |
} | |
return results; | |
}; | |
var example = "tacocat"; | |
var list = example.split(''); | |
console.log(perm(list, [])); | |
//console.log(perm(['c', 'i'], ['a'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment