EDIT: I will try to be more clear in this question:
I have this array [1,2,3]
and I want to generate all permutations like this:
1,2,3 | 1,3,2 | 3,2,1 | 3,1,2 | 2,3,1 | 2,1,3 | 1 | 1,2 | 1,3 | 2 | 2,3 | 2,1 | 3 | 3,1 | 3,2
Do note that we also want single and double digit permutations, which is not addressed in the proposed duplicate.
I tried to adapt Python code to JavaScript:
function my_permutations(lst) {
if (lst.length == 0) {
return []
}
if (lst.length == 1) {
return [lst]
}
var l = []
var m;
var remLst;
for (var i = 0; i < lst.length; i++) {
m = lst[i]
remLst = lst.slice(0, i).concat(lst.slice(i + 1))
my_permutations(remLst).forEach(function(element) {
l.push([m].concat(element))
});
}
return l
}
console.log(JSON.stringify(my_permutations([1, 2, 3])))
With the new edit in the code I can get all combination of triplet ! But I want more, I want combination with pairs and singletons. How can I do that ?
So not all possible combinations are generated.
How can I fix this?