title: Finding all permutations of array elements as concatenated strings
body: <p>I am trying to write a JavaScript function that returns all combinations of the elements of an array of unknown length. The argument to be passed to the function should be an array of single digit strings e.g. <code>[ &quot;5&quot;, &quot;7&quot;, &quot;9&quot; ]</code>.</p>
  <p>An example to illustrate the desired functionality:</p>
  <ul>
  <li>If you pass in an array of <code>[ &quot;5&quot;, &quot;7&quot;, &quot;9&quot; ]</code>, it should return an array with all the possible 3-digit combinations of those 3 numbers i.e. <code>[ &quot;579&quot;, &quot;759&quot;, &quot;957&quot;, &quot;795&quot;,</code>…<code>]</code>.</li>
  <li>If you passed in an array of <code>[ &quot;2&quot;, &quot;5&quot;, &quot;4&quot;, &quot;6&quot; ]</code>, you would get the 4-digit combinations of those 4 numbers, i.e. <code>[ &quot;2546&quot;, &quot;2654&quot;, &quot;2465&quot;,</code>…<code>]</code>.</li>
  <li>If you passed in an array of length 5, you would get the 5-digit combinations of those 5 numbers, and so on.</li>
  <li>If the inputted array has the same digit multiple times, that number should appear multiple times in the resulting array e.g. an input of <code>[ &quot;5&quot;, &quot;5&quot;, &quot;6&quot; ]</code> produces an output of <code>[ &quot;556&quot;, &quot;655&quot;, &quot;565&quot;,</code>…<code>]</code>.</li>
  </ul>
  <p>I have looked around and it seems that recursion might be the way to go, but I am struggling to get it working. I have attempted the below solution which currently works for 3-digit numbers but I can’t figure out how to make a function which works with an array of unknown length.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getDoubleDigitCombinations(input) {
    let result = [];
    const first = input[0];
    const last = input[1];
  
    result.push(first + last);
    result.push(last + first);
  
    return result;
  }
  
  
  function getTripleDigitCombinations(input) {
    let result = [];
    let main; // This is the number in question.
    let other1; // These are the other numbers.
    let other2;
  
    for (i = 0; i &lt; input.length; i++) {
      let arr = input.slice(); // Make a copy of the input array.
      
      main = input[i];
      arr.splice(i, 1); // Remove the main element from the array …
      other1 = arr[0]; // … so that you are left with the other two numbers.
      other2 = arr[1];
  
      console.log(`Main is ${main}`);
      console.log(`other1 is ${other1} and other2 is ${other2}`);
  
      const arr2 = getDoubleDigitCombinations([other1, other2]); // Get the combinations of the others.
  
      result.push(main + arr2[0]); // Concatenate main with both of the others combinations.
      result.push(main + arr2[1]);
    }
  
    return result;
  }
  
  let result2 = getTripleDigitCombinations([ "6", "7", "8" ]);
  
  console.log("result2 is ...");
  
  for (i = 0; i &lt; result2.length; i++) {
    console.log(result2[i]);
  }</code></pre>
  </div>
  </div>
  </p>