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

**URL:** <https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710>\
**Category:** General\
**Created:** [September 27, 2016, 9:53pm UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710 "2016-09-27T21:53:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![FireInTheNorth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fireinthenorth/32/15999_2.png) [@FireInTheNorth](https://forum.freecodecamp.org/u/FireInTheNorth)\
**Post date:** [September 27, 2016, 9:53pm UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/1 "2016-09-27T21:53:32Z")

</div>

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

```auto
Definitely not working solution

```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 28, 2016, 1:48am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/2 "2016-09-28T01:48:39Z")

</div>

FCC checker doesn’t like global variables.

---

<div class="post-metadata">

**Author:** ![FireInTheNorth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fireinthenorth/32/15999_2.png) [@FireInTheNorth](https://forum.freecodecamp.org/u/FireInTheNorth)\
**Post date:** [September 28, 2016, 12:34pm UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/3 "2016-09-28T12:34:11Z")

</div>

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

```auto
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. 😏

---

<div class="post-metadata">

**Author:** ![slocodemonkey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/slocodemonkey/32/15110_2.png) [@slocodemonkey](https://forum.freecodecamp.org/u/slocodemonkey)\
**Post date:** [September 29, 2016, 4:04pm UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/4 "2016-09-29T16:04:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![verv0eren](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/verv0eren/32/10106_2.png) [@verv0eren](https://forum.freecodecamp.org/u/verv0eren)\
**Post date:** [October 4, 2016, 3:55am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/5 "2016-10-04T03:55:42Z")

</div>

@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??

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [October 4, 2016, 4:10am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/6 "2016-10-04T04:10:31Z")

</div>

> <https://github.com/FreeCodeCamp/FreeCodeCamp/issues/9199>

---

<div class="post-metadata">

**Author:** ![verv0eren](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/verv0eren/32/10106_2.png) [@verv0eren](https://forum.freecodecamp.org/u/verv0eren)\
**Post date:** [October 4, 2016, 5:30am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/7 "2016-10-04T05:30:27Z")

</div>

@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.

```auto

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

```

@jenovs any idea, what should i do here?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [October 4, 2016, 6:09am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/8 "2016-10-04T06:09:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![verv0eren](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/verv0eren/32/10106_2.png) [@verv0eren](https://forum.freecodecamp.org/u/verv0eren)\
**Post date:** [October 4, 2016, 6:10am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/9 "2016-10-04T06:10:07Z")

</div>

hmm thanx jenovs, I will think along that line.

---

<div class="post-metadata">

**Author:** ![slocodemonkey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/slocodemonkey/32/15110_2.png) [@slocodemonkey](https://forum.freecodecamp.org/u/slocodemonkey)\
**Post date:** [October 4, 2016, 9:21pm UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/10 "2016-10-04T21:21:03Z")

</div>

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){
        counter++;
        console.log(counter);
        myRec(counter); // this is recursion!
    }
    
}

myRec(0);
```

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:30am UTC](https://forum.freecodecamp.org/t/strange-bug-with-steamroller-challenge-solution-what-did-i-wrong-intermediate-algorithm-scripting/40710/11 "2021-01-16T05:30:13Z")

</div>

Closed on January 16, 2021, 5:30am UTC


