Failing 3, 4, 4, 7, 8.
Could someone explain what the prefix value is and what it’s meant to do? There’s not much mention of it in the user stories, and I think understanding it might help me write code that actually functions.
function permuteString(string, prefix = "", results = []) {
if (string.length === 0) {
results.push(prefix);
let resultsSet = new Set(results);
let resultsArray = new Array(resultsSet);
return resultsArray;
};
for (let character in string) {
let removed = string[character];
prefix += removed;
permuteString(string.substring(1,), prefix, results);
};
}
console.log(permuteString("walk"));
Do I have the base case wrong? Prefix gets longer than the original string and all the function returns is undefined.
while prefix changes based on character, string.substring(1,) is always the same
also, you add characters to prefix at each iteration of the loop, instead you need a different prefix based on character. using the machine example you have one iteration of the loop where prefix is m and string.substring(1,) is achine, and then at the next iteration of the loop prefix is ma, and string.substring(1,) is still achine, instead you need at the second iteration a prefix of a and the string to permute be mchine
also, you need a return also when string.length === 0 is false
I don’t understand that last bit about when to return- if I’m returning when string.length === 0 and when string.length === 0 is false, aren’t I always returning?
that is correct, that is the base case, which is when the function does not call itself again and all the functions that are pending start getting resolved
the return value can’t be only in the base case, because then the other function calls would return undefined
This code now does something, but only ever adds one letter to the prefix. How do I determine when the prefix is long enough to add to the results array?
function permuteString(string, prefix = "", results = []) {
if (string.length === 0) {
results.push(prefix);
let resultsSet = new Set(results);
let resultsArray = new Array(resultsSet);
return resultsArray;
};
for (let character in string) {
prefix = string[character];
let newString = string.substring(0, character) + string.substring(character + 1,);
console.log(newString);
console.log(prefix);
permuteString(newString, prefix, results);
results.push(prefix);
return results;
};
}
if you remove one chracter from the string and add one character to the prefix so that their lengths always sum to the same number, the prefix has the right length when string is empty
if their lengths don’t always sum to the same number, somthing is going wrong
This code only iterates through the string once, so the only result in the array at the end is the word itself, with all of the letters in the correct order. Why is this?
function permuteString(string, prefix = "", results = []) {
if (string.length === 0) {
results.push(prefix);
prefix = "";
return results;
};
for (let character in string) {
prefix += string[character];
let newString = string.substring(0, character) + string.substring(character + 1,);
console.log(`On character ${string[character]} with prefix ${prefix} and testing string ${newString}`);
permuteString(newString, prefix, results);
return results;
};
}
do you understand that a return inside the loop makes the loop not work? so where does the return go?
second thing, it seems you know how to concatenate two strings together without changing the original string, you are doing so for newString, you need to do it for prefix