What is wrong with this recursive function?

I’m working on the steamroller challenge. I actually got this code (or something very similar? It’s possible I may have changed it unwillingly) to work, but because the function I wrote takes two arguments it didn’t pass the tests. So I decided to simply remove it and put it into its own function, and then I can pass it whatever arguments I want.

// takes two arguments. First is the array to
// flatten. arr2 is an empty array for storing
// the results.
function smasher(arr1,arr2){
  
// step through every element in arr1
for(i = 0; i < arr1.length; i++){
      
  // if it is NOT an array, push it into arr2 as it is.
      if(Array.isArray(arr1[i]) === false){
        arr2.push(arr1[i]);
        
        // if it IS an array, run it through this function again
        // then loop through the array it returns (x) and 
        // push all those values to arr2
      } else {
        var x = smasher(arr1[i],[]);
        for(j = 0; j < x.length; j++){
          arr2.push(x[j]);
        }
      }
}
  return arr2;
}

However, now no matter what I do, running this locks up the FCC page. What’s worse, it even locks up if I feed it a simple, non-nested array such as [1,2,3,4,5], so it should never even branch into the “else” statement and hit the recursive code.

I’ve been thinking about the logic here all day, and I simply cannot see where it is failing.

I don’t see anything wrong with it? I submitted it and it works for me

Can you post exactly what you’re submitting? I feel like there might be an issue in how you call Smasher

Alright, well, thank you for looking into it! You are probably right that it has something to do with how I was calling smasher, but at this point I’ve already gone and researched a different method of solving the problem (using Array.prototype.reduce, which was what I wanted to use instinctively at the start, anyway).

I’m less concerned about solving the challenge, and more with figuring out whether or not my logic was screwy. I feel much more at ease if other people tell me that I’m not crazy when I say this thing looks like it should work, haha.

Your logic seems fine, but there is a lot of unnecessary cruft there which could be cleaned up.

This seems like a code smell, you’re repeating code that in theory does the same thing the original code does. I havn’t played with it byond that but it seems like you’re writing a two -level recursive function, rather than one. Not sure if that makes sense. I may play with this later today if I have time.