Need help with Solution 4

Hi guys so I was looking through Solution 4 and I can’t understand why “flatArr = ” was passed as a parameter to the function and why “flatArr” is also passed as an argument in the if statement when calling the function steamrollArray. Could anyone help?
Your code so far


function steamrollArray(val,flatArr=[]) {
val.forEach(item => {
  if (Array.isArray(item)) steamrollArray(item, flatArr);
  else flatArr.push(item);
});
return flatArr;
}

steamrollArray([1, [2], [3, [[4]]]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Steamroller

Link to the challenge:

Whoever wrote this solution decided to change the function signature to take a second argument. The = [] bit provides a default value if the argument isn’t passed in.

This is not a good way to solve the problem.

1 Like

Thanks for the reply. I get it now.

I’m glad I could help. Happy coding!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.