Solving "Steamroller" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-07-31 11:32:44

---
title: Solving "Steamroller" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "Steamroller" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-032-steamroller
series: Solving freeCodeCamp's Algorithm Challenges
---

!['Steamroller'](//images.ctfassets.net/yzwpq1epaq68/64pTCHl5vHjJM6886s9oDx/5ceffdaa69e43ae503b1e83b946b75a8/ss-032.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Steamroller'.



### Starter Code

```javascript
function steamrollArray(arr) {
  return arr;
}

steamrollArray([1, [2], [3, [[4]]]]);
```



### Instructions

Flatten a nested array. You must account for varying levels of nesting.



### Test Cases (& Rules)

- `steamrollArray([[["a"]], [["b"]]])` should return `["a", "b"]`.
- `steamrollArray([1, [2], [3, [[4]]]])` should return `[1, 2, 3, 4]`.
- `steamrollArray([1, [], [3, [[4]]]])` should return `[1, 3, 4]`.
- `steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4]`.
- ***Your solution should not use the `Array.prototype.flat()` or `Array.prototype.flatMap()` methods.***



# Our Approach

After reading the instructions, starter code, and test cases more than once, this is what we're working with:

- Our function takes in one argument, `arr`, which is an array of subarrays (contains numbers, strings, objects).
- We must return the array flattened (see test cases).
- Must do this without methods such as `flat()` or `flatMap()`.



Looking at all the test cases, we have some interesting cases like `[[4]]]` as an index in `arr`. We need to go visit each index and remove them from an array, if they are in one.



The first thing I will do is create an empty array, to hold our new flattened array.



```javascript
let flattened = [];
```



As we have to visit each index of `arr`, I figured to use the method, `map()`.



[MDN: Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map "MDN Docs: Array.map()")



```javascript
arr.map((val) => {
  // more code coming
})
```





What are we going to do at each index though? There is another array method, `isArray()`, to check if the value is an array or not. It will return `true` or `false`.



[MDN: Array.isArray()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray "MDN Docs: Array.isArray()")



If the value in the index is not an array, we can add it into our new `flattened` array.



```javascript
arr.map((val) => {
  if (!Array.isArray(val)) {
    flattened.push(val);
  }
  else {
    // see below
  }
})
```



That is not too complex. For our `else` statement, how are we handling the indexes which contain an array? 



We can use the `...` operator in the `else` statement on `steamrollArray()` so it will be called and flatten the array each loop. This is a recursive fashion (I believe).

```javascript
arr.map((val) => {
  if (!Array.isArray(val)) {
    flattened.push(val);
  }
  else {
    flattened.push(...steamrollArray(val));
  }
})
```



If our `arr` is `[[[1]], 2]`, our first `val` is `[[1]]`. It will not pass the `if` statement since it is in an array so it will be evaluated by the `else` statement. We're calling the same `flattened.push` but not on the `val`. We're pushing `...steamrollArray(val)`, which will run the function again but it flattens `val` with each execution. Once it is out of the subarray, it will be pushed into `flattened`.



Make sure to return `flattened`.



# Our Solution

```javascript
function steamrollArray(arr) {
  let flattened = [];

  arr.map(val => {
    if (!Array.isArray(val)) {
      flattened.push(val);
    }
    else {
      flattened.push(...steamrollArray(val));
    }
  })

  return flattened;
}
```



# Links & Resources

['Steamroller' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller "Intermediate Algorithm Scripting: Steamroller on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/032---steamroller.js "GitHub Solution")



Thank you for reading!