I want to make a functions that prints all the possible combinations from the given string. example: given_string = "ABC" this should print: AAA AAB AAC ABA ABB ABC .. and so on, until it reaches maximum options. CCC
I found some code on the web and modified it for JS
s = ["A", "B", "C"]
function printAllKLength(set, k) {
n = set.length;
printAllKLengthRec(set, "", n, k);
}
function printAllKLengthRec(set, prefix, n, k) {
if (k == 0) {
console.log(prefix);
return;
}
for (i = 0; i < n; i++) {
newPrefix = prefix + set[i];
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
printAllKLength(s, 4)
It only changes the last character and I don't understand where is my mistake. original code URL: https://www.geeksforgeeks.org/print-all-combinations-of-given-length/