Strange bug with Steamroller challenge solution - What did I wrong? [Intermediate Algorithm Scripting]

Strange bug with Steamroller challenge solution - What did I wrong? [Intermediate Algorithm Scripting]


Can you please tell me why FCC doesn’t accept my solution?

Definitely not working solution


FCC checker doesn’t like global variables.


Ok, so this is my new solution. I’m much closer but I have strange bug…

function steamrollArray(startingArray) {
  var result = [];
  function steamrollArrayInside(arr) {
    if( Array.isArray(arr) ) {
      arr.forEach(function() {
        //Inside the loop you just concat the steamrolled elements to the result.
        result = result.concat(flattendArray(arr)); 
      return result; 
    } else {
      return arr;
  function flattendArray(input) {
    if( Array.isArray(input) ) {
      return input.forEach(flattendArray);
    } else {
      return result.push(input);
  return steamrollArrayInside(startingArray);
steamrollArray([1, [2], [3, [[4]]]]); 

Should return [1, 2, 3, 4], but it returns [ 1, 2, 3, 4, undefined, 1, 2, 3, 4, undefined, 1, 2, 3, 4, undefined ]!

And I don’t know why. :smirk:


It’s because you’re trying to solve this using loops (forEach) instead of recursion.

Basically, you have an array of objects, and some of those objects are arrays. So you want to loop through the array, and if the object is ANOTHER array, you want to send that array back to the original function. That’s what recursion is.

Look through array - > If element is NOT an array, push the element to a result array -> if element IS an array, send it to the beginning of this line. -> Repeat this process until we’ve looped through all the elements of the original array.

Right? I hope that helps.


@FireInTheNorth @jenovs
I was gonna ask the same. I used a global variable in my code, and it’s giving the result. But FCC is not accepting it. Wonder why FCC checker doesn’t like global variables??



@slocodemonkey can’t push to the result array as we are not allowed to have a global array. While using a local result array, every recursive call of the function will reset it.

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

@jenovs any idea, what should i do here?


And what if you make another function inside the main function?


hmm thanx jenovs, I will think along that line.


This is another advantage of recursion. You can create a local variable, and then pass it back to the recursive function.

Try this code as an example of what I’m talking about:

function myRec(counter){
    if(counter < 5){
        myRec(counter); // this is recursion!