Please help me to understand recursion better and the solution to the stamrollArray challenge. I have already watched different videos, but they all have only a basic explanation. I would be very thankful for your help.
After looking at the solutions of other people, I could come up with this solution, but I still do not understand why in order to call up the function again for recursion, It has to be pushed into a new array together with a spread operator (newArr.push(…steamrollArray(arr[i])))?
Why can’t I simply call up the function by writing (steamrollArray(arr[i])) without pushing it into a new array with a spread operator?
As far as I could understand if I simply call the function without pushing it into a new array, the new array will be empty with every new recursion.
If I am right, I really would like to know what happens behind the scene when I push the function into a new array? And why it must be pushed together with a spread operator?
Thank you in advance.
What we are doing here is once we reach a case where we have a plain value instead of an array, we get that, push it into an array, and return it.
So for each base case we get back a value shaped like this: [x] .
But since we don’t want to push the value as it is, but its content, we spread it and push it into our result.