curriculum/challenges/english/blocks/lab-deep-flattening-tool/ab306dbdcc907c7ddfc30830.md
In this lab, you will be implementing an array flattening algorithm.
Flattening an array means turning a nested array of any depth into a single, one-dimensional array. The process extracts all elements in order, unwrapping only arrays. Other types are left unchanged.
For example:
| Original | Flattened |
|---|---|
[[1], [], [2, [3]]] | [1, 2, 3] |
[1, {"foo": "bar"}, [2]] | [1, {"foo": "bar"}, 2] |
["baz", [1, 2], {}] | ["baz", 1, 2, {}] |
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
steamrollArray.steamrollArray function should accept one argument: a nested array.Array.prototype.flat() or Array.prototype.flatMap() methods.You should have a steamrollArray function.
assert.isFunction(steamrollArray);
steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].
assert.deepEqual(steamrollArray([[['a']], [['b']]]), ['a', 'b']);
steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].
assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
steamrollArray([1, [], [3, [[4]]]]) should return [1, 3, 4].
assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods.
const arr = [1,2,3,[1,2,3,[1,2,3]]];
const spyFlat = __helpers.spyOn(Array.prototype, 'flat');
try {
steamrollArray(arr);
assert.isEmpty(spyFlat.calls);
} finally {
spyFlat.restore();
}
const spyFlatMap = __helpers.spyOn(Array.prototype, 'flatMap');
try {
steamrollArray(arr);
assert.isEmpty(spyFlatMap.calls);
} finally {
spyFlatMap.restore();
}
You should not use global variables.
steamrollArray([1, {}, [3, [[4]]]])
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4])
function steamrollArray(arr) {
if (!Array.isArray(arr)) {
return [arr];
}
const out = [];
arr.forEach(function(e) {
steamrollArray(e).forEach(function(v) {
out.push(v);
});
});
return out;
}