How to set the length of a generated permutation?
for example:
permutator(['as', 'dd', 'ff'], 2);
This is what I got so far:
function permutator(inputArr, lngth){
let results = [];
function permute(arr, mem){
let cur, memo = mem || [];
for (let i = 0; i < arr.length; i++) {
cur = arr.splice(i, 1);
if (arr.length === 0) {
results.push(memo.concat(cur).join(''));
}
permute(arr.slice(), memo.concat(cur));
arr.splice(i, 0, cur[0]);
}
return results;
}
return permute(inputArr);
}
console.log(permutator(['as','dd','ff'], 2));
This returns by 3 but not in 2 permutations:
["as,dd,ff", "as,ff,dd", "dd,as,ff", "dd,ff,as", "ff,as,dd", "ff,dd,as"]
i want it to return something like this:
["as,dd", "as,ff", "dd,as", "dd,ff", "ff,as", "ff,dd", ...................... ]