Is my code is not efficient?

I can’t pass the test, is because bug, or my code is not efficient?
I guess my code is enough efficient because only 15 lines :slight_smile:

Here is the code


let Arr = [];
function steamrollArray(arr) {
// console.log(arr)
arr.forEach(element => {
  if (!Array.isArray(element)) {
    Arr.push(element)
  }else if (Array.isArray(element)) {
    steamrollArray(element)
  }
})
return Arr;
}

console.log(steamrollArray([[["a"]], [["b"]]]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:79.0) Gecko/20100101 Firefox/79.0.

Challenge: Steamroller

Link to the challenge:

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
1 Like

Thank you so much, finally I know the problem!