# Steamroller InterMediateAlgorithm

**URL:** <https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800>\
**Category:** General\
**Created:** [June 8, 2017, 2:18am UTC](https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800 "2017-06-08T02:18:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theb205](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@theb205](https://forum.freecodecamp.org/u/theb205)\
**Post date:** [June 8, 2017, 2:18am UTC](https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800/1 "2017-06-08T02:18:53Z")

</div>

```
var result= [];
function steamrollArray(arr) {
   for(var i = 0; i < arr.length; i++){
       if(!Array.isArray(arr[i])) result.push(arr[i]);
       else steamrollArray(arr[i]);
   }
  return result;
}
steamrollArray([1, {}, [3, [[4]]]]);

```

It works all cases, but why not pass?

---

<div class="post-metadata">

**Author:** ![psychometry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/psychometry/32/79114_2.png) [@psychometry](https://forum.freecodecamp.org/u/psychometry)\
**Post date:** [June 8, 2017, 2:33am UTC](https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800/2 "2017-06-08T02:33:09Z")

</div>

why are you using a global variable?

add another  
`steamrollArray([1, {}, [3, [[4]]]]);`  
to your code and you’ll see what’s wrong;

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [June 8, 2017, 2:25pm UTC](https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800/3 "2017-06-08T14:25:26Z")

</div>

You are using a global variable.

Global variables persist after function calls have completed, so we have to be very careful about modifying globals in functions. Your code relies on `result` being an empty array when the function is called, but after `steamrollArray` has been executed it is no longer an empty array. This means that your function will only work once.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 6:03pm UTC](https://forum.freecodecamp.org/t/steamroller-intermediatealgorithm/124800/4 "2021-01-16T18:03:11Z")

</div>

Closed on January 16, 2021, 6:03pm UTC


