Skip to content

(17) New exercise | Flatten #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 17_flatten/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Exercise 17 - flatten

Write a function that takes in an array of positive integers and an optional depth argument which defaults to `Infinity` if not specified.

The function returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

```javascript
flatten([[1, 2], [3, 4]]); // Output: [1, 2, 3, 4]
flatten([[1, [8, 9]], [3, 4]], 2); // Output: [1, [8, 9], 3, 4]
flatten([2, 4, 6]); // Output: [2, 4, 6]
```
6 changes: 6 additions & 0 deletions 17_flatten/flatten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const flatten = function() {

};

// Do not edit below this line
module.exports = flatten;
15 changes: 15 additions & 0 deletions 17_flatten/flatten.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const flatten = require('./flatten');

describe('flatten', () => {
test('First test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(flatten()).toBe('');
});

test.skip('Second test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(flatten()).toBe('');
});
});
13 changes: 13 additions & 0 deletions 17_flatten/solution/flatten-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const flatten = function (array, depth = Infinity) {
if (depth === 0 || !array.some(Array.isArray)) return array

let flattened = []
array.forEach(el => {
flattened = flattened.concat(el)
});

return flatten(flattened, depth - 1)
}

// Do not edit below this line
module.exports = flatten;
17 changes: 17 additions & 0 deletions 17_flatten/solution/flatten-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const flatten = require('./flatten-solution');

describe('flatten', () => {
test('Works', () => {
expect(flatten([[1, [2, 3]], 4], 1)).toEqual([1, [2, 3], 4])
})
test('Works with a depth argument', () => {
expect(flatten([ [1, 2], [3, 4, [[1, [8, 9]], [3, 4]], 2] ], 2)).toEqual([1, 2, 3, 4, [1, [ 8, 9 ]], [3, 4], 2]);
});

test('Works with no depth argument', () => {
expect(flatten([[[1, 2], [3, 4]], 1, [[[[[[[[1]]]]], 4, [[[[[[5, 4]]]]]], 1]]]])).toEqual([1, 2, 3, 4, 1, 1, 4, 5, 4, 1]);
});
test('Works with an empty array', () => {
expect(flatten([])).toEqual([]);
});
});