From 2443377b5ba5611b317f5c7c032dc0009782939e Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 15:05:50 +0000 Subject: [PATCH 1/5] feat/ add new exercise --- 17_flatten/README.md | 3 +++ 17_flatten/flatten.js | 6 ++++++ 17_flatten/flatten.spec.js | 15 +++++++++++++++ 17_flatten/solution/flatten-solution.js | 6 ++++++ 17_flatten/solution/flatten-solution.spec.js | 15 +++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 17_flatten/README.md create mode 100644 17_flatten/flatten.js create mode 100644 17_flatten/flatten.spec.js create mode 100644 17_flatten/solution/flatten-solution.js create mode 100644 17_flatten/solution/flatten-solution.spec.js diff --git a/17_flatten/README.md b/17_flatten/README.md new file mode 100644 index 00000000000..cabd3c10767 --- /dev/null +++ b/17_flatten/README.md @@ -0,0 +1,3 @@ +# Exercise 13 - flatten + +Description of the exercise goes here. diff --git a/17_flatten/flatten.js b/17_flatten/flatten.js new file mode 100644 index 00000000000..91f21103161 --- /dev/null +++ b/17_flatten/flatten.js @@ -0,0 +1,6 @@ +const flatten = function() { + +}; + +// Do not edit below this line +module.exports = flatten; diff --git a/17_flatten/flatten.spec.js b/17_flatten/flatten.spec.js new file mode 100644 index 00000000000..b8c64ccbcaf --- /dev/null +++ b/17_flatten/flatten.spec.js @@ -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(''); + }); +}); diff --git a/17_flatten/solution/flatten-solution.js b/17_flatten/solution/flatten-solution.js new file mode 100644 index 00000000000..9a7076048ea --- /dev/null +++ b/17_flatten/solution/flatten-solution.js @@ -0,0 +1,6 @@ +const flatten = function() { + // Replace this comment with the solution code +}; + +// Do not edit below this line +module.exports = flatten; diff --git a/17_flatten/solution/flatten-solution.spec.js b/17_flatten/solution/flatten-solution.spec.js new file mode 100644 index 00000000000..8ae84795780 --- /dev/null +++ b/17_flatten/solution/flatten-solution.spec.js @@ -0,0 +1,15 @@ +const flatten = require('./flatten-solution'); + +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('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(flatten()).toBe(''); + }); +}); From 0e15670f56dbbc95178406eba2101243a6c6d636 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 16:20:35 +0000 Subject: [PATCH 2/5] feat/ update readme --- 17_flatten/README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/17_flatten/README.md b/17_flatten/README.md index cabd3c10767..86d8024b104 100644 --- a/17_flatten/README.md +++ b/17_flatten/README.md @@ -1,3 +1,10 @@ -# Exercise 13 - flatten +# Exercise 17 - flatten -Description of the exercise goes here. +Write a function that takes in an array 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] +``` From ba8b9eabf150c0bac4a1591b1354cbea93316784 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:14:57 +0100 Subject: [PATCH 3/5] feat: update README to be more descriptive --- 17_flatten/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/17_flatten/README.md b/17_flatten/README.md index 86d8024b104..c1544421cdb 100644 --- a/17_flatten/README.md +++ b/17_flatten/README.md @@ -1,6 +1,7 @@ # Exercise 17 - flatten -Write a function that takes in an array and an optional depth argument which defaults to `Infinity` if not specified. +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 From d880a5075e67f36f5e3dde5d43cc6660fbf20262 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:15:14 +0100 Subject: [PATCH 4/5] feat: add solution for exercise --- 17_flatten/solution/flatten-solution.spec.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/17_flatten/solution/flatten-solution.spec.js b/17_flatten/solution/flatten-solution.spec.js index 8ae84795780..5b262667bb1 100644 --- a/17_flatten/solution/flatten-solution.spec.js +++ b/17_flatten/solution/flatten-solution.spec.js @@ -1,15 +1,17 @@ const flatten = require('./flatten-solution'); 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('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('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - - expect(flatten()).toBe(''); + 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([]); }); }); From 07ba41675e79eb9c6693d3bdf2ea559f88cce2b7 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:15:41 +0100 Subject: [PATCH 5/5] feat: create test files for exercise --- 17_flatten/solution/flatten-solution.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/17_flatten/solution/flatten-solution.js b/17_flatten/solution/flatten-solution.js index 9a7076048ea..5e1c2782cc4 100644 --- a/17_flatten/solution/flatten-solution.js +++ b/17_flatten/solution/flatten-solution.js @@ -1,6 +1,13 @@ -const flatten = function() { - // Replace this comment with the solution code -}; - +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;