2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
id: ab306dbdcc907c7ddfc30830
|
|
|
|
title: Steamroller
|
|
|
|
isRequired: true
|
|
|
|
challengeType: 5
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 16079
|
2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
Flatten a nested array. You must account for varying levels of nesting.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2018-10-20 21:02:47 +03:00
|
|
|
- text: <code>steamrollArray([[["a"]], [["b"]]])</code> should return <code>["a", "b"]</code>.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(steamrollArray([[["a"]], [["b"]]]), ["a", "b"]);
|
2018-10-20 21:02:47 +03:00
|
|
|
- text: <code>steamrollArray([1, [2], [3, [[4]]]])</code> should return <code>[1, 2, 3, 4]</code>.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
|
2018-10-20 21:02:47 +03:00
|
|
|
- text: <code>steamrollArray([1, [], [3, [[4]]]])</code> should return <code>[1, 3, 4]</code>.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
|
2018-10-20 21:02:47 +03:00
|
|
|
- text: <code>steamrollArray([1, {}, [3, [[4]]]])</code> should return <code>[1, {}, 3, 4]</code>.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
|
2020-02-05 03:00:33 +05:00
|
|
|
- text: Your solution should not use the <code>Array.prototype.flat()</code> or <code>Array.prototype.flatMap()</code> methods.
|
|
|
|
testString: assert(!code.match(/\.flat\([\s\S]*?\)/) && !code.match(/\.flatMap\([\s\S]*?\)/));
|
2018-10-04 14:37:37 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function steamrollArray(arr) {
|
|
|
|
// I'm a steamroller, baby
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
steamrollArray([1, [2], [3, [[4]]]]);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```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;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|