Problem in streamroller challenge

Problem in streamroller challenge
0.0 0

#1

Why is my code not passing the given test when it gives the expected output


var final = [];

function steamrollArray(arr) {
  // I'm a steamroller, baby
  
  for (var i=0; i<arr.length; i++) {
    checkArray(arr[i]);
  }
  
  return final;
  
}

function checkArray(a) {
  if (Array.isArray(a)) {
    performLoop(a);
  } else {
    final.push(a);
  }
}

function performLoop(newArr) {
  for (var i=0; i<newArr.length; i++) {
    checkArray(newArr[i]);
  }
}

steamrollArray([1, {}, [3, [[4]]]]);

Link to the challenge:


#2

The first test will pass. However, because you have a global variable, final, that global variable now contains the values from the first test. The next test will add to that already-populated array, and so on.

Put the var final = [] in your main function, and either inline checkArray into the function as well or pass the final array as an argument to it. Global variables will generally break the tests.