Skip to content

Commit 6e966da

Browse files
committed
Project W1(B)
Array Section & Extra Credit Completed.
1 parent 9793db4 commit 6e966da

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/arrays.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ const filter = (elements, cb) => {
5555
const flatten = (elements) => {
5656
// Flattens a nested array (the nesting can be to any depth).
5757
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
58-
const newArr = [].concat(...elements);
58+
const newArr = [];
59+
for (let i = 0; i < elements.length; i++) {
60+
if (elements[i].length) {
61+
elements = elements.concat(elements[i]);
62+
} else {
63+
newArr.push(elements[i]);
64+
}
65+
}
5966
return newArr;
6067
};
6168

0 commit comments

Comments
 (0)