title: Recursive selection sort (JS)
body: <p>I'm writing recursive selection sort in JavaScript.</p>
  
  <p><strong>Expecting behavior</strong>: I want the function <code>selectionSort()</code>to sort values in array in ascending order.</p>
  
  <p><strong>Problem</strong>: I cannot get out from recursion and I don't know how.</p>
  
  <p><strong>Error</strong>:</p>
  
  <blockquote>
    <p><code>Uncaught RangeError: Maximum call stack size exceeded</code></p>
  </blockquote>
  
  <p><hr />
  Here is my code:</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>const findSmallestIndex = ( arr ) =&gt; {
      let smallest = arr[0];
      let smallestIndex = 0;
      let arrLen = arr.length;
  
      for ( let i = 0; i &lt; arrLen; i++ ) {
          if ( arr[i] &lt; smallest ) {
              smallest = arr[i];
              smallestIndex = i;
          }
      }
      return smallestIndex;
  };
  
  const selectionSort = ( arr ) =&gt; {
      let smallest = arr.splice( findSmallestIndex( arr ), 1 );
      return [smallest].concat( selectionSort( arr ) );
  };
  
  let arr = [23, 43, 23423, 66, 5, 57, 78, 0, 1];
  
  console.log( selectionSort(arr) );</code></pre>
  </div>
  </div>
  </p>