Steamroll Array Challenge - I've spent days

Tell us what’s happening:
During my first iteration of forEach(), the push works as expected, but the second time, I get an error message. I cannot see what has changed between iterations.

Your code so far


function steamrollArray(arr) {
let item = 0;
let finalArray = [];
let loopingArray = [];
  finalArray = flatten(arr, loopingArray);
  return finalArray;
}

function flatten(arr, loopingArray) {
arr.forEach(arrayItem => {
  console.log(arrayItem);
  if (Array.isArray(arrayItem)) {
    console.log(arrayItem, 'flattening again');
    flatten(arrayItem);
  }
  else {
    console.log(arrayItem, 'being pushed');
    loopingArray.push(arrayItem);
    console.log('current looping Array', loopingArray);
  }
})
console.log(loopingArray);
return loopingArray;
}

steamrollArray([1, [2], [3, [[4]]]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Steamroller

Link to the challenge:

Hello there,

This is an issue of:
Here is your function definition:

function flatten(arr, loopingArray) {

Here is the function call:

flatten(arrayItem);

Can you see the problem?

Hope this helps

1 Like

YES. Thank you so much.