Daily Coding Challenge - Permutation Count, loop doesn't continue

Tell us what’s happening:

When I call the “permutate” function inside my loop, the loop doesn’t continue to the next iteration and I don’t know what to do about it

Your code so far

function countPermutations(str) {
  const letters = {};
  const letterCounts = {};
  const arr = [];
  let counter = 0;
  str = str.split("");

  for (const curr of str){
    if (letters[curr] == undefined){
      letters[curr] = 0;
      letterCounts[curr] = 1;
      continue;
    }
    letterCounts[curr]++;
  }
  const copy = {...letters};

  permutate(str.length, "", copy, 0);
  
  function permutate(n, curr, object, c){
    if (arr.indexOf(curr) !== -1) return;
    if (c === n){
      arr.push(curr);
      counter++;
      return;
    }
    let count = 0;
    const keys = Object.keys(object);
    for (const curr of Object.values(object)){
      count += Number(curr);
    }
    keys.forEach((letter) => {
      console.log(object[letter] !== letterCounts[letter])
      if (object[letter] !== letterCounts[letter]){
        object[letter]++;
        permutate(n, curr + letter, object, c + 1);
      }
    })
  }
  console.log(arr);

  return counter;
}

console.log(countPermutations("abb"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:157.0) Gecko/20100101 Firefox/157.0

Challenge Information:

Daily Coding Challenge - Permutation Count
https://www.freecodecamp.org/learn/daily-coding-challenge/12-04
GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69162d64f96574d9bb629f01.md at main · freeCodeCamp/freeCodeCamp · GitHub

I realized that I didn’t need the “count” variable so I removed it and the loop too. Here’s the updated code:

function countPermutations(str) {
  const letters = {};
  const letterCounts = {};
  const arr = [];
  let counter = 0;
  str = str.split("");

  for (const curr of str){
    if (letters[curr] == undefined){
      letters[curr] = 0;
      letterCounts[curr] = 1;
      continue;
    }
    letterCounts[curr]++;
  }
  const copy = {...letters};

  permutate(str.length, "", copy, 0);
  
  function permutate(n, curr, object, c){
    if (arr.indexOf(curr) !== -1) return;
    if (c === n){
      arr.push(curr);
      counter++;
      return;
    }

    const keys = Object.keys(object);
    
    keys.forEach((letter) => {
      console.log(object[letter] !== letterCounts[letter])
      if (object[letter] !== letterCounts[letter]){
        object[letter]++;
        permutate(n, curr + letter, object, c + 1);
      }
    })
  }
  console.log(arr);

  return counter;
}

console.log(countPermutations("abb"));

Here’s what the function is supposed to do:

  1. takes string and turns it into an array
  2. makes two objects:
    • the first object saves all the possible letters with a counter always set to 0
    • the second object saves the number of occurences for the letters
  3. initializes the counter for the tests to succeed
  4. starts the “permutate” function which takes 4 arguments:
    • the first one is the length of the string which was input in the “countPermutations” function
    • the second one is the current permutation, e. g. “ab”, “bb”, whatever
    • the third one is the first object, which will increment as it goes through the recursion
    • the fourth one will be the counter for the current permutation,
  5. when the permutate function starts, it checks if the current permutation is already inside the array, if it is it immediatly exits out
  6. it then checks if the 1st and 4th argument are the same number, aka if the permutation now has the length of the input array. if it does it pushes it into the array to save it for the condition before, and it also increments the counter.
  7. then the function saves the object keys for the loop
  8. the loop checks, if the current letter already has used up all of its instances, if it doesn’t it increments the object to use it again when the function calls itself.

but then the loop doesn’t continue, when I call the function and I don’t know why

where is this variable from? you use it in permutate but do not define it

Hi @Ninge3007,

Try logging arr.indexOf(curr) as the first line of your permutate function. What do you see in the console?

Happy coding

it is always -1, what am I supposed to see?

I defined it at line 5

I askes Sokrates and he gave me the hint, that I had to decrement the value after the recursion call, this finally made the code work for tests 1-3, but now the problem is, that the code times out before it finished for the final test.

Here’s the updated code:

function countPermutations(str) {
  const letters = {};
  const letterCounts = {};
  let counter = 0;
  str = str.split("");

  for (const curr of str){
    if (letters[curr] == undefined){
      letters[curr] = 0;
      letterCounts[curr] = 1;
      continue;
    }
    letterCounts[curr]++;
  }

  permutate(str.length, "", letters, 0);
  
  function permutate(n, curr, object, c){
    if (c === n){
      counter++;
      return;
    }
    
    Object.keys(object).forEach((letter) => {
      if (object[letter] !== letterCounts[letter]){
        object[letter]++;
        permutate(n, curr + letter, object, c + 1);
        object[letter]--;
      }
    })
  }

  return counter;
}

When I ran the code in VSCode, it gave me the correct result, so I don’t know what I can do to improve my code for the online IDE.

That’s what happened to me when I did this challenge. I had to get online help to pass the tests on this one. I looked up “javascript count unique permutations of a string efficiently” to get at a solution that uses factorial math and doesn’t require saving the permutations to an array.