Skip to content

Commit a2c697b

Browse files
committed
Complete steamroller algorithm
1 parent c024ea1 commit a2c697b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,15 @@ export function dropElements(arr, func) {
353353
return arr.slice(arr.indexOf(element));
354354
}
355355
```
356+
357+
#### Steamroller
358+
359+
Flatten a nested array. You must account for varying levels of nesting.
360+
361+
```javascript
362+
export function steamrollArray(arr) {
363+
return arr.reduce((acc, val) => {
364+
return Array.isArray(val) ? acc.concat(steamrollArray(val)) : acc.concat(val)
365+
}, []);
366+
}
367+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function steamrollArray(arr) {
2+
return arr.reduce((acc, val) => {
3+
return Array.isArray(val) ? acc.concat(steamrollArray(val)) : acc.concat(val)
4+
}, []);
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { steamrollArray } from "../../src/fcc-intermediate-algorithms/steamroller";
2+
3+
test('should flatten an array', () => {
4+
expect(steamrollArray([[["a"]], [["b"]]])).toEqual(["a", "b"]);
5+
expect(steamrollArray([1, [], [3, [[4]]]])).toEqual([1, 3, 4]);
6+
});

0 commit comments

Comments
 (0)