SteamRoller not passing the tests

Tell us what’s happening:
As far as I can tell, my code is outputing the correct arrays but it is not passing any of the tests. Am I missing something? I know this is maybe not to best practices but I feel like it should still work.
Your code so far


const newArr = [];

function steamrollArray(arr) {

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

console.log(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/79.0.3945.117 Safari/537.36.

Challenge: Steamroller

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller

I don’t completely understand why, but your console.log is throwing it off. Perhaps it is running and populating the array with bad data each time. One solution would be to have a wrapping helper function to invoke the recursion, where the array would have scope (instead of globally). Another solution is to comment out that console.log.

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2