What's wrong with my code: recursive

function steamrollArray(arr) {
  // I'm a steamroller, baby
 
 //loop through each item in arr
  for(let x=0;x<arr.length;x++){
    if(Array.isArray(arr[x])){
      steamrollArray(arr[x]); //if arr[x] is an array. recursive i.e call func again
    }else
      myArr.push(arr[x]); //pushing arr[x] if it is not an array.
  }
  return myArr;
}
//declear a global array to store result;
 let myArr = [];

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

It gives me correct ans But i still can’t pass the challenge

You’re trying to use myArr before it’s initialised, so I think you’re going to get a ReferenceError here. However, once that’s fixed it still won’t work.

So for the first test you’ll be fine, but the second test will now start with an array full of values from the last test, third test will start with the values from the first test and the second test and so on, so the tests will fail. You can’t use global variables like that in the algorithm challenges of FCC, they will always break the tests.

@DanCouper
How i’m trying to use global variable before initializing it.

let myArr = [];  //first initialize global variable

steamrollArray(arr);   // calling a function which will use the global variable i already initilized

You use it in the function declaration, which comes before the let, so it shouldn’t be available within the closure when you call the function (I may be wrong on this with respect to browser implementation, but that’s how let/cost are supposed to behave)

Edit:

function works() {
  let x = 1;
  return x;
}

function shouldntWork() {
  return x;
  let x = 1;
}

What you’re doing is the second one, you’re using something then declaring it.

As it is that’s all irrelevant because you’re using a global variable so you’ve broken the tests anyway