Build a Permutation Generator - Build a Permutation Generator

Tell us what’s happening:

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.

Your code so far

function permuteString(string, prefix, results = []) {
  if (string.length === 0) {
    results.push(prefix);
    return results;
  };
  string.forEach((character) => {
    string = string.substring(1,);
    permuteString(string, character, results);
  })
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Permutation Generator - Build a Permutation Generator
https://www.freecodecamp.org/learn/javascript-v9/lab-permutation-generator/build-a-permutation-generatore over each character in the input string and for

there will be new instructions soon, let me post those here, tell me if they help

Thank you! That’s a lot clearer

1 Like

This is my new code

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?

yes, you need to always have a return

otherwise how would permuteString('machine') output a value? in this case str’s length is not 0

I thought string.length === 0 was the base case

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

Oh yes, of course. Thanks for clearing that up!

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;
  };
}

you should not do this, you are changing the length of prefix without changing the lenght of the string passed in to permuteString

also because a return inside a loop makes the loop not work

let newString = string.substring(0, character) + string.substring(character + 1,);

Does this not change the length of the string being passed in?

if you fix the return issue so that the loop works

without considering the recursion part
then you get that in the three iterations of the loop

character is 0
prefix.length is 1
newString.length is 2

but then when character is 1
prefix += string[character]; makes prefix.length 2, and newString.length is also 2

and when character is 2, prefix.length is 3, newString.length is still 2

I still don’t understand how to fix that.

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

I understand the thing about the loop. What I don’t understand is what I’m meant to do with prefix.

don’t change the value of prefix

pass the new value to the recursive function call without changing the value of the prefix variable