Question about Steamroller and using helper functions

Hi, this is my code so far:

var global = [];

function recursive(input) {
  for (var i = 0; i < input.length; i++) {
    if (Array.isArray(input[i])) {
      recursive(input[i]);
    }
    else {
      global.push(input[i]);
    }
  }
}

function steamrollArray(arr) {
  // I'm a steamroller, baby
  recursive(arr);
  return global;
}

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

The above code is getting the correct outputs for every suggested input but I’m getting red X’s for each test. I decided to use the helper function to recursively go through the code w/o assigning a return value and used a global variable to push the correct value to the final array. Is this not allowed in the programming assignment despite giving the correct answer?

I’m not sure why you aren’t passing with that code, but you have the right idea. Instead of having a global array, think about how you can wrap all of this up into one function. It may be failing some internal check, and it’s a really bad idea to use globals anyways.

I don’t think that’s the issue because the first test doesn’t pass when the tests are made, but when I test it by running it myself the correct response gets output. If I’m using the helper function I can change the parameters and return an array like PortableStick suggested, I’ll see if that fixes it. Either way thank you both for the replies.

I am not saying that putting the global array inside the function will create a passing solution. I am just telling you that the global array keeps growing between each test and that is why you are not getting the correct answer. See your solution with some added console.log statements so you can see what the global variable starts with at the beginning of each call to recursive function.

https://repl.it/@rmdawson71/globalVariableCausingTrouble

Sorry for the delay, you are definitely right member variables are not necessary and can be bad because of accumulated problems. When I rewrote the code to use parameters instead it worked overall so thank you both for the help.