Intermediate Algorithm Scripting - Steamroller (my solution is failing tests)

“Flatten a nested array. You must account for varying levels of nesting.”
I tried to use recursion in my solution:

let newArr = [];
function steamrollArray(arr) { 
  for(let i = 0; i < arr.length; i++) {
    if(Array.isArray(arr[i]) == false) {
    } else {
  return newArr;

This seems to work, because it always returns the answers it should return (e.g. steamrollArray([1, [2], [3, [[4]]]]) returns [1, 2, 3, 4]), but all of the tests fail. Can anyone help?

could you also include that exercise link as well for more context, thanks :slight_smile:

also try using “three backticks” for pasting over “codes” that helps in readability as well, happy learning :slight_smile:

You can not use (and should not use) global variables. The freeCodeCamp tests run sequentially and make multiple calls to your function. Since newArr is a global variable, then each time the function gets called for other arrays, newArr’s value will not reset back to an empty array for the other tests. You can definitely use recursion to solve this challenge, you just can not use global variables to accomplish it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.