Steamroller challenge question

I’ve made this solution for the challenge, and the result seems to be right to me but the test feature of the page seems not to agree with that :confused:

const newArr = []
function steamrollArray(arr) {
  for (let elem of arr){
    if(Array.isArray(elem))
      steamrollArray(elem)
    else{
      newArr.push(elem)
    }
  }
  return newArr
}
const result = steamrollArray([1, [2], [3, [[4]]]])
console.log(result);

I mean, I got confused with that. Because my function is returning the right value - [1,2,3,4], in this case - but the page is not accepting it

You can not declare newArr as a global variable, because the tests run your code and keep calling the steamrollArray function multiple times. This means each time the function is called, newArr will start with the value it ended with in the previous ran test. In general, it never good to use global variables, because you never know how another part of an application may use a variable.

Thanks man, I’ve refactored my code and I not used global variable this time, then it worked just fine…

function steamrollArray(arr) {
  
  function recursive(mainArr,newArr) {
    
    for (let elem of mainArr) {
      if (Array.isArray(elem))
        recursive(elem,newArr)
      else {
        newArr.push(elem)
      }
    }
    return newArr
    
  }
  return recursive(arr,[])
}

console.log(steamrollArray([1, [2], [3, [[4]]]]))

I Appreciated your help o/