Steamroller test bug

In Steamroller challenge I think there is a bug. I get the correct answer for each test, and in my browser, but the in app test do not says so. Is there a problem with this assignment?

No. If you think there is you should raise it as a bug.

Where can I raise one?

Can you explain what is the bug?

I test each of the given tests, and all return the correct answer. But when I click the “Run Tests” button nothing happens. It does not return anything.

@luis-alves Probably a problem with your browser or computer. Did you try resetting the test?

Tried in firefox and chrome with the same result.

My code is this:

var newArray = [];
function steamrollArray(arr) {
  // debugger;
  // I'm a steamroller, baby
  for (var i = 0; i < arr.length; i++) {
    // Base case
    if (!Array.isArray(arr[i])) {
      newArray.push(arr[i]);
    } else {
      // Recursion case
      steamrollArray(arr[i]);
    }

  }
  return newArray;
}

From my memory, the tests do not like global variables. I know this is not clear so your code is working. Bear this in mind for future challenges.

OK, ok! Got it. Will correct. Thanks.

Most likely it is the global newArray that is not re-initialized between test cases - call your recursive function from an initial wrapper function that initializes a local newArray and returns it

A very similar question

I got it to work with this ugly script. Is there prettier way?

var newArray = [];

function rollEach(arr) {
  for (var i = 0; i < arr.length; i++) {
    if (!Array.isArray(arr[i])) {
      newArray.push(arr[i]);
    } else {
      rollEach(arr[i]);
    }
  }
  return newArray;
}

function steamrollArray(arr) {
  // debugger;
  var finalArray = [];
  finalArray = rollEach(arr);
  newArray = [];

  return finalArray;
}

It’s a good idea in general to avoid global variables at all costs - not just because of the side effect here on multiple test cases - you still have a global newArray

An easy way is simply to keep finalArray in scope of rollEach - something like

function steamrollArray(arr) {
  let finalArray=[]
  function rollEach(arr) {
     ...
  }
  return finalArray
}

Another way is to remove the need for an external array to fill - instead build and return the array recursively so the final return has the final array

My solution can be found here [Edited]

I’m getting Error 500 accessing your site.

Thanks ppc. Ended with this final version:

function steamrollArray(arr) {
  var newArray = [];

  var finalArray = [];
  finalArray = rollEach(arr);

  function rollEach(arr) {
    for (var i = 0; i < arr.length; i++) {
      if (!Array.isArray(arr[i])) {
        newArray.push(arr[i]);
      } else {
        rollEach(arr[i]);
      }
    }
    return newArray;
  }

  return finalArray;
}

Looks better - is newArray still needed?

Also while rollEach returns an array its real work is a side-effect to fill an external array - in fact the return value of the recursive call is discarded - you can improve the code by not relying on the external array at all - it may require a bit of thought -
maybe revisit this solution after you have completed more exercises