Help me with the Steamroller Intermediate Algorithm please. :)

Tell us what’s happening:
Hi, so the question was to flatten/steamroll the array. And I know there are numerous answers in the hint section but I’m wondering why my answer isn’t working.
Thanks in advance :slight_smile:

Your code so far


var tab=[];
function steamrollArray(arr) {
  // I'm a steamroller, baby

for(let i=0;i<arr.length;i++){

if(Array.isArray(arr[i])){
steamrollArray(arr[i]);

}
else{
  tab.push(arr[i]);
}


}

  return tab;
}
steamrollArray([[["a"]], [["b"]]]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0.

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

it’s an interesting approach, though I’d really advise against it

using a global variable like that is a bit nasty, at the very least it should be captured in another function body or something

If steamRollArray is called multiple times, e.g. steamrollArray([[["a"]], [["b"]]]); steamrollArray([[["c"]], [["d"]]]); what do you expect to happen?

Aha, It’ll start saving all the values from both the function calls. Thank you @gebulmer :slight_smile: