Steamroller - please explain a line of code to me :)

Here is my solution to the problem

function steamrollArray(arr) {
  // I'm a steamroller, baby
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      result = result.concat(steamrollArray(arr[i]));
    } else {
      result.push(arr[i]);
    }    
  }
  return result;
}

However, the logic of the line
result = result.concat(steamrollArray(arr[i]));
I got from googling and found it on stackoverflow. I do understand it’s recursion, and I do understand the logic partly, but not as clearly as I would like to.
It would be greatly appreciated if anyone gives me a clear explanation what really happens here. Thanks forward!

etc
arr visually looks something like this

arr[0] = [ [1,2], [3,4], [5,6] ]
arr[1] = [ [7,8] ]

if u do not go in recursion
your result with result.push(arr[i])
will be
[ [ [1,2], [3,4], [5,6] ], [ [7,8] ] ]

but with this recursion u pass every single child that is array
so when u pass that child ( that is array ), it goes into it’s children and checks is there any child that is array, and so on…
…and when finally it came to child that is not array it push into result array, and back result to the control
then it all concat into one big array

so after ur algo it looks like
[ 1, 2, 3, 4, 5, 6, 7, 8 ]

1 Like

If an element i is an array, you call the same steamroller function on element i. Now you check all elements of element i, and if any of those elements are arrays, the function is called again on that element.

This keeps on going till all elements of a passed array are NOT arrays, and then control is given back to the calling element. The result array will thus always contain non array elements, as you keep recursing till you get an array without arrays in it.

For a better idea of how recursion works, look up stacks. Essentially every function call is pushed into the stack and when a base result is reached ( which is when we get an array without arrays inside it here), the top most call’s popped off the stack and given control with the returned value.

I’m not that great at explaining but I hope this helps till a better answer comes along!

2 Likes

Recursion is one of those things that are better understood informally rather than formally. Just think of what your steamrollArray(arr) is trying to do. It looks at each element of arr and asks: Is this also an Array like arr itself?

  • Yes? Okay. Then, as it is the same kind of thing, I need to do the same kind of stuff to it (steamroll it) before I throw its contents into result.

  • No? Well, in that case, it is just a plain old value and I can throw it into result as it is.

Just forget about the term recursion and look at it as an analogy: for ‘Yes’, since you are doing exactly the same thing to arr[i] as to arr, it makes sense to call the exact same function on arr[i] as on arr. Basically in programming, doing the same thing corresponds to calling the same function.

1 Like

Thank you all for explanations! They are wonderful! :slight_smile:
@ZeroXLR Especially. Your explanation’s superb! I really get it now :slight_smile:

No problem at all. Explaining things helps me consolidate my own knowledge too.

1 Like