I'm generating all combinations of an array, so for instance, ["a", "b", "c", "d"]
will generate:
[
"a", "b", "ab", "c", "ac",
"bc", "abc", "d", "ad", "bd",
"abd", "cd", "acd", "bcd", "abcd"
]
Here's the code I've written that does complete this task.
What I'd like to know is if there is a better way, as iterating over the array twice feels like I'm cheating, or the complexity of the code is much more computationally expensive than it needs to be.
Also, the name for a function that takes an array and returns the combinations, what might that be called? Combinator seems inappropriate.
var letters = ["a", "b", "c", "d"];
var combi = [];
var temp= "";
var letLen = Math.pow(2, letters.length);
for (var i = 0; i < letLen ; i++){
temp= "";
for (var j=0;j<letters.length;j++) {
if ((i & Math.pow(2,j))){
temp += letters[j]
}
}
if (temp !== "") {
combi.push(temp);
}
}
console.log(combi.join("\n"));