2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
id: ab306dbdcc907c7ddfc30830
|
|
|
|
title: Steamroller
|
|
|
|
challengeType: 5
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 16079
|
2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
2020-03-26 20:13:34 +05:00
|
|
|
|
2018-10-04 14:37:37 +01:00
|
|
|
Flatten a nested array. You must account for varying levels of nesting.
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2020-03-26 20:13:34 +05:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`steamrollArray([[["a"]], [["b"]]])` should return `["a", "b"]`.
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert.deepEqual(steamrollArray([[['a']], [['b']]]), ['a', 'b']);
|
|
|
|
```
|
|
|
|
|
|
|
|
`steamrollArray([1, [2], [3, [[4]]]])` should return `[1, 2, 3, 4]`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
|
|
|
|
```
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`steamrollArray([1, [], [3, [[4]]]])` should return `[1, 3, 4]`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
|
|
|
|
```
|
2020-03-26 20:13:34 +05:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4]`.
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
|
2018-10-04 14:37:37 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Your solution should not use the `Array.prototype.flat()` or `Array.prototype.flatMap()` methods.
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(!code.match(/\.\s*flat\s*\(/) && !code.match(/\.\s*flatMap\s*\(/));
|
|
|
|
```
|
2020-03-26 20:13:34 +05:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function steamrollArray(arr) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
steamrollArray([1, [2], [3, [[4]]]]);
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function steamrollArray(arr) {
|
|
|
|
if (!Array.isArray(arr)) {
|
|
|
|
return [arr];
|
|
|
|
}
|
|
|
|
var out = [];
|
|
|
|
arr.forEach(function(e) {
|
|
|
|
steamrollArray(e).forEach(function(v) {
|
|
|
|
out.push(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
```
|