title: all anagrams - recursion
body: <p>I'm trying to understand the following recursive function below to teach myself recursion. My attempt to explain it is below the expected output and the function itself. what am I missing? I'm not seeing the process where you get from 'abc' to 'acb'. my attempt to understand it got me from 'abc' right to 'bac'.</p>
  
  <pre><code>    example usage:
          var anagrams = allAnagrams('abc');
          console.log(anagrams); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]
  
  
  
  
  
          var allAnagrams = function(string) {
  
            var uniqueOutput = {};
  
            (function anagram(ana, str) {
  
              // could have also written this as: if(!str)....
              if (str === '') {
                uniqueOutput[ana] = 1;
              }
              //recursive call for the length of the anagram.
              for (var i = 0; i &lt; str.length; i++) {
                anagram(ana + str[i], str.slice(0, i) + str.slice(i + 1));
                console.log(ana);
              }
            })('', string);
            console.log(uniqueOutput)
            return Object.keys(uniqueOutput);
          };
  
  
   // you are calling the recursive function like this: anagram([anagram=]'', [string=]'abc')
      // so the base case is not met on the first iteration since the string isn't empty
      // first iteration: i = 0
      // anagram('' + 'a' + 'bc' [from str.slice(0 +1)]) ---&gt; resolves to "abc") ---&gt; anagram("abc")
      //second iteration: i = 1. we are still in the original call from line 37.
      // here below, does "ana" stay as '' since we are still inside the initial recursion call?
      //anagram('' + b + a +  c) ---- resolves to "bac" ---&gt; anagram("bac")
      //third and last iteration iteration: i = 2
      //anagram(" " + c + c) ----&gt; anagram("cc") ? not a valid result.
  </code></pre>
  
  <p>My new, correct (I think) explanation:</p>
  
  <pre><code>//initial input: anagram("", "abc")
  //STEP 1: enter the function --&gt; i = 0 for original string "abc"
    //anagram("" + "a", "bc") ----&gt; anagram("a", "bc")
  //STEP 2: loop through the new, modified string, which is now "bc"
      //var i = 0;
      //anagram("a" + "b", "c")---&gt; anagram("ab", "c")
      //anagram("ab" + "c", [nothing here])
        //base case hit, so uniqueOutput["abc"] = 1;
      //var i = 1; --&gt; this applies to the string "bc". the loop gets reset to i = 0 once you have a new string to worth with (like below, with "b")
      //anagram("a" + "c", "b")
      //anagram("ac", "b")
      //anagram("ac" + "b", "" )
        //base case hit, so uniqueOutput["acb"] = 1;  
  //STEP 3: increment up on the original string input ("abc") --&gt; so now you are dealing with str[i] === b
    //var i =  1;
    //anagram("" + "b", "a" + "c")
      //anagram("b", "ac")  ---&gt; now we need to loop through "ac"!
        //anagram("b" + "a", "c")
          //anagram("ba", "c") 
            //anagram("bac", "")---&gt; base case hit, uniqueOutput["bac"] = 1;
      //anagram("b", "ac")
        //anagram("b" + "c", "a") ---&gt; anagram("bc", "a")
          //anagram("bca", "") ---&gt; base case hit, uniqueOutput["bca"] = 1;
  //STEP 4: increment up on the original string input ("abc") ---&gt; str[i] === c
    //var i = 2;
    //anagram ("" + "c", "ab")---&gt; anagram("c", "ab")
      //now we need to loop through "ab!" c's index stays the same.
      //anagram("c" + "a", "b") ---&gt; anagram("ca", "b")
        //anagram("cab", '')---&gt; uniqueOuput["cab"] = 1;
      //anagram("c" + "b", "a") ---&gt; anagram("cb", "a")
        //anagram("cba", "")----&gt; uniqueOutput["cba"] = 1
  </code></pre>