title: Javascript fixing my flatten(array) function
body: <p>I can not figure out why my code does not flatten out the nested arrays as indicated. I'd greatly appreciate some help here. I used a recursion to get to the actual value of the nested array. I tried to debug my code, and it seems to replace my results array every time the recursion takes place.</p>
  
  <pre><code>//Helper methods
  
  function toType(obj){
      return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  }
  
  function each(collection, callback){
      if (Array.isArray(collection)){
          for (var i = 0; i &lt; collection.length; i++){
              callback(collection[i], i, collection)
          }
      } else {
          for (var i in collection){
              callback(collection[i], i, collection)
          }
      }
  }
  
  //Flatten function
  
  function flatten(array, isShallow=false, callback){
      var results = [];
      each(array, function(item){
          if (!!isShallow &amp;&amp; toType(item) === 'array'){
              each (item, function(value){
                  results.push(value);
              })
          } else if (toType(item) !== 'array'){
              results.push(item);
          } else {
              return flatten(item)
          }
      })
      return results;
  }
  
  flatten([1, [2], [3, [[4]]]]);
  
  // ---&gt; [1]
  </code></pre>