How does this code work? JS



  • How does finalPermutations 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;
    }



  • I'll try to explain, but if you don't understand, I strongly suggest you go to the book and read it carefully. https://learn.javascript.ru/recursion ♪

    Let's say we have a challenge: permutations('123');

    Condition. ♪ ♪

    if (string.length <= 1) {
        return [string];
      }
    

    it turns out wrong and we're on the next line. let finalPermutations = permutations(string.substring(1)), where the function causes itself, as follows: let finalPermutations = permutations('23')♪ There's a first step in the field.

    And then everything goes the same way that the function is called: let finalPermutations = permutations('3')♪ And in this challenge, we have reached a base of perception, that is, when the functions no longer need to be called upon. She's going into a situation that, in fact, looks like,

      if ('3'.length <= 1) {
        return ['3'];
      }
    

    Now we're moving back in the field. Inside the challenge. permutations('23') The base of the pcury turns this challenge. let finalPermutations = permutations('3').reduce((acc, p) => ...)specific to this: let finalPermutations = ['3'].reduce((acc, p) => ...)

    Next, it's starting to work on a collbeck ofheart, which will end up in the next top of the field: ['23, '32'].

    Since the next upper level of the artsy is the point of entry into the field at all, the final result of the function will be a mass: ['123', '213', '231', '132', '312', '321'].

    That's the same thing I tried to explain, just in the form of a scheme:

                        шаг рекурсии                         база рекурсии
    premutations('123') ------------> premutations('23') --> premutations('3')
                                                                    |
                                                             результат базы рек.        
                                        ['3'].reduce()  <--    ['3']
                                            |
                                    здесь мы принимаем
                                    результат базы рекурсии
                                    и начинаем отрабатывать
                                    дальше по функции, и получаем
                                             |
            ['23', '32'].reduce()  <-- ['23', '32']
                     |
                 Конечный результат.
           ['123', '213', '231', '132', '312', '321']
    

    P. S. If you don't understand how we get a result after the end of the method. reducehere, I recommend that every method of massivian or line used in it be carefully read and that each step within the cycle be monitored independently.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2