Steamroller Answer is Correct but Not passing the test

Tell us what’s happening:
My code give correct answer but it is not passing even a single test??
Can anyone explain why is it so?
Can I use arr.flat(Infinity) it gives answer in single line of code??

Your code so far


let answ = []
function steamrollArray(arr) {
  for(let i = 0; i< arr.length; i++) {
    let value = arr[i]
    if(Array.isArray(value)) {
      steamrollArray(value)
  	}
  	else answ.push(value)
  }
	return answ
}
console.log(steamrollArray([1, [2], [3, [[4]]]]))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

You have a global variable, all the tests will make a new function call that change the same global variable. You shouldn’t use a global variable

Whats the problem with global variable, it push the value only when it is non array or just a single value other than that it will not push any value into the answ array

but each of the different function calls will push to the global variable

The function call in your code works correctly

steamrollArray([1, [2], [3, [[4]]]])  // returns [ 1, 2, 3, 4 ]

But after that there are the function calls used by the tests to verify that your code works. Look, I have added what each function returns, maybe you can see the issue.

steamrollArray([[["a"]], [["b"]]])    // returns [ 1, 2, 3, 4, 'a', 'b' ]
steamrollArray([1, [2], [3, [[4]]]])  // returns [ 1, 2, 3, 4, 'a', 'b', 1, 2, 3, 4 ]
steamrollArray([1, [], [3, [[4]]]])   // returns [ 1, 2, 3, 4, 'a', 'b', 1, 2, 3, 4, 1, 3, 4 ]
steamrollArray([1, {}, [3, [[4]]]])   // returns [ 1, 2, 3, 4, 'a', 'b', 1, 2, 3, 4, 1, 3, 4, 1, {}, 3, 4 ]