my test is logging the correct result, but I feel like for some reason when I call the method it does’nt return the correct array, what am I missing here?
Your code so far
function permuteString(string, prefix = '', array = []){
if(string.length === 0) {
array.push(prefix);
if(array.length == getPermuteAmount(prefix.length))
{
console.log([...new Set(array)]);
return [...new Set(array)];
}
}
for(const char in string){
let s = string.split("");
s.splice(char, 1);
s = s.join("");
permuteString(s, prefix + string[char], array);
}
}
function getPermuteAmount(length){
let amm = 0;
let len = length;
while(len > 0){
amm += len;
len--;
}
return amm;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
I think we may have to open an issue to discuss this exercise. I gave it to Science on discord and she’s struggling with it too. I think the requirement to use a single recursive function (with no helper) makes it extra complex.
This is a tough challenge indeed but I wrestled with it for some time and eventually figured it out.
Precisely because I found it so tough (and recursion can be a headache), I also wrote a very detailed breakdown of exactly how it works.
If anyone’s interested in seeing it, please let me know!
I am interested in your breakdown because my code doesn’t go all the way, the recursion ends too soon and I think it’s because I don’t really know how to go about the challenge. But anyway, I am interested in your breakdown…
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.