title: Flatten nested arrays using recursion (and without using loops)
body: <p>My logic for the problem, using the below as the input. </p>
  
  <pre><code>var input = [['A','B'],1,2,3,['C','D']]
  </code></pre>
  
  <ol>
  <li>Check first element to see if is an Array or not using Array.isArray(input)</li>
  <li>If first element is array, call function, first element ['A,'B'] as argument.</li>
  <li>The first element of the nested array is 'A' which is not an array, so push this element into a result array, and shift this element out. Repeat the function call. </li>
  </ol>
  
  <p>When trying to flatten nested arrays using recursion, my input variable to the function keeps getting reassigned, preventing me from calling the function again using the original array. How do I prevent the original input variable from getting reassigned? </p>
  
  <p>I understand this is not the complete solution, however I am stuck at when I shift the first element out of the nested array.</p>
  
  <p>I've gone through step by step with this function, but there must be something I'm missing, another set of eyes would help greatly. </p>
  
  <p>I've also been using my chrome developer tool, set breakpoints to monitor the function step by step. </p>
  
  <pre><code>//Defining original input variable
  var input = [['A','B'],1,2,3,['C','D']]
  
  function flat(array){
      var result = []
      var firstElement = array[0]
  
  //CHECK IF FIRST ELEMENT IS ARRAY OR NOT
      if(Array.isArray(firstElement)){
      return flat(firstElement)  
      } 
  
  
  //IF ELEMENT NOT ARRAY, PUSH ELEMENT TO RESULT
      else{result.push(firstElement)
      array.shift() //removing child element
      return (flat(array)) //call function on same array
      }
  
  if(array.length===0){return result}
  
  }
  </code></pre>
  
  <p>First iteration:
  firstElement = ['A','B'], Array.isArray(firstElement) would be true, hence call flat(firstElement)</p>
  
  <p>Second Iteration: 
  firstElement = 'A', Array.isArray(firstElement) is false, so we 
  1. jump down to push this element into result
  2. remove 'A' by using array.shift() 
  3. Call flat(array), where array is now ['B']</p>
  
  <p>Third Iteration: 
  firstElement = 'B', Array.isArray(firstElement) is false
  1. jump down to push this element into result, result is now only ['B'] since I've reset the result when I recalled the function. 
  2. remove 'B' by using array.shift(), array is now empty, ->[ ]
  3. How can I step out, and use flat() on the original input array? </p>