Steamroller test bug

Steamroller test bug
0

#1

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?


#2

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


#3

Where can I raise one?


#4

Can you explain what is the bug?


#5

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.


#6

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


#7

Copy/Paste your code into a reply post, so we can see the code you have. It could be related to how you have written the code.


#8

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;
}

#11

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.


#12

Because you are declaring newArray as a global variable (outside the function), when the tests are run, newArray still has items in it when the next test runs. The best way to see this is putting console.log for each test when running the code in your browser and then looking at the console to see what is happening:

console.log(steamrollArray([[["a"]], [["b"]]]));
console.log(steamrollArray([1, [2], [3, [[4]]]]));
console.log(steamrollArray([1, [], [3, [[4]]]]));
console.log(steamrollArray([1, {}, [3, [[4]]]]));

#13

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


#14

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


#15

Just moving the newArray = [] inside the function will still not solve the challenge, but at least you understand why it was not working when the tests were running.


#16

A very similar question


#17

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;
}

#20

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


#21

My solution can be found here [Edited]


#22

I’m getting Error 500 accessing your site.


#23

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;
}

#24

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