I don’t understand how permutations work

I don’t understand how finalPermutation work.

function permutations(string) {
  if (string.length <= 1) {
    return [string];
  }
  
  let finalPermutations = permutations(string.substring(1))
    .reduce((acc, p) => {
      let charList = p.split('');
      for (let i = 0; i <= charList.length; i++) {
        let newPermutation = charList.slice(0, i)
                              .concat([string[0]])
                              .concat(charList.slice(i))
                              .join('');
        if (!acc.includes(newPermutation)) {
          acc.push(newPermutation);
        } 
      }
      return acc;      
  },[]);
  return finalPermutations;
}

hi @blog8043604268 ,

here’s the code breakdown explanation, and I hope it can be clear for your understanding.

function permutations(string) {
  if (string.length <= 1) {
    return [string];
  }
  
  /**
   * Package the function into a instance ' finalPermutations ' and return it
   * The function take in a string value and start off at array position 2,
   * which is equivalent as 1 in .substring function
   * While .reduce function, it takes in (acc = previous value, p = current value)
   * It helps to perform multiple callbacks until all string value has been checked
   * by later adding it back into the ' acc '.
   * Also take note, at the end of reduce function, there's a starting initial value,
   * in this case, it's an empty array [ ] to begin with.
   */
  let finalPermutations = permutations(string.substring(1))
    .reduce((acc, p) => {

      let charList = p.split('');
      for (let i = 0; i <= charList.length; i++) {

        /**
         * The process here done is ...
         * 1 -- crop the array from index 0 to loop declared index variable " i "
         * 2 -- combine into multi-dimension array (e.g., [ [ array 1], [ array 2 ] ] 
         * 3 -- combine with slice charList array of selected index
         * 4 -- lastly, join the arrays into one string value
         */
        let newPermutation = charList.slice(0, i)
                              .concat([string[0]])
                              .concat(charList.slice(i))
                              .join('');

        /**
        * Check if the new pattern has always exist within ' acc '
        * If yes, add the pattern into previous value (accumulate, in a sense)
        */
        if (!acc.includes(newPermutation)) {
          acc.push(newPermutation);
        } 
      }
      return acc;      
  },[]);
  return finalPermutations;
}

// Sample for you to run.
permutations('abcd');
function permutations(string) { if (string.length <= 1) { return [string]; } let finalPermutations = permutations(string.substring(1)) .reduce((acc, p) => { let charList = p.split(''); for (let i = 0; i <= charList.length; i++) { let newPermutation = charList.slice(0, i) .concat([string[0]]) .concat(charList.slice(i)) .join(''); if (!acc.includes(newPermutation)) { acc.push(newPermutation); } } return acc; },[]); return finalPermutations; } // Sample for you to run. permutations('abcd');

Additionally, do look up certain built-in JavaScript functions to aid your learning and reference to them whenever you require as they are awesomely helpful.

If the above documentations aren’t helpful, do look for other sources as well. (e.g., W3schools)

Hope this helps you in your understanding :smile: