Intermediate Algorithm Scripting - Steamroller (my solution is failing tests)

“Flatten a nested array. You must account for varying levels of nesting.”
I tried to use recursion in my solution:

let newArr = [];
function steamrollArray(arr) { 
  for(let i = 0; i < arr.length; i++) {
    if(Array.isArray(arr[i]) == false) {
      newArr.push(arr[i]);
    } else {
      steamrollArray(arr[i])
    }
  }
  return newArr;
}

This seems to work, because it always returns the answers it should return (e.g. steamrollArray([1, [2], [3, [[4]]]]) returns [1, 2, 3, 4]), but all of the tests fail. Can anyone help?

could you also include that exercise link as well for more context, thanks :slight_smile:

also try using “three backticks” for pasting over “codes” that helps in readability as well, happy learning :slight_smile:

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