Steamroller code works but not on FreeCodeCamp

Tell us what’s happening:
My code works, yet it doesn’t work on FreeCodeCamp. Can anyone help?

Your code so far

var newarr=[];
function steamrollArray(arr) {
  // I'm a steamroller, baby
  for (let i=0; i<arr.length;i++){
    if (Array.isArray(arr[i])){
    } else {
  return newarr;

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/72.0.3626.119 Safari/537.36.

Link to the challenge:

The function steamrollArray gets called over and over again in the tests. What is the value of newarr the second (and third and fourth…) time the function is called? (Hint: it’s not [])

To put it another way, the task is to write a function that flattens an array. I can see the variable newarr isn’t part of that function, it’s just kinda floating around outside it. If I delete that variable, will the function still work?

No the function will not work if newarr is deleted. Yes I don’t want to make newarr =[ ] everytime I call the function, that is why I declared it outside.

You can not use global variables with the challenge, because the same variable would be used for all the test cases, so the global variable would start the 2nd test with the value it had at the end of the first test and so on.

You can see this in action if you write console.log(newarr); as the first line in the function and then open your browser’s console.

1 Like

Ok I see. Thanks for your help.