Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

80 lines
1.5 KiB
Markdown

---
id: ab306dbdcc907c7ddfc30830
title: Steamroller
challengeType: 1
forumTopicId: 16079
dashedName: steamroller
---
# --description--
Flatten a nested array. You must account for varying levels of nesting.
# --hints--
`steamrollArray([[["a"]], [["b"]]])` should return `["a", "b"]`.
```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]);
```
`steamrollArray([1, [], [3, [[4]]]])` should return `[1, 3, 4]`.
```js
assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
```
`steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4]`.
```js
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
```
Your solution should not use the `Array.prototype.flat()` or `Array.prototype.flatMap()` methods.
```js
assert(!__helpers.removeJSComments(code).match(/\.\s*flat\s*\(/) && !__helpers.removeJSComments(code).match(/\.\s*flatMap\s*\(/));
```
Global variables should not be used.
```js
steamrollArray([1, {}, [3, [[4]]]])
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4])
```
# --seed--
## --seed-contents--
```js
function steamrollArray(arr) {
return arr;
}
steamrollArray([1, [2], [3, [[4]]]]);
```
# --solutions--
```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;
}
```