From a7477ccc9e8a7aaeaca15f23c1f7ce670a5c17b4 Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:23:35 +0530 Subject: [PATCH 1/6] Created FlattenedArray.js Flatten Array using recursive method --- Recursive/FlattenedArray.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Recursive/FlattenedArray.js diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js new file mode 100644 index 0000000000..eb8560bac4 --- /dev/null +++ b/Recursive/FlattenedArray.js @@ -0,0 +1,17 @@ +const flattened = (arr) => { + const = [] + arr.forEach(i => { + if(Array.isArray(i)){ + res.push(...flattened(i)); + } + else{ + res.push(i); + } + }); + return res; +} + +(() => { + const arr = [['a'],['bc'],[['cde'], ['f']]]; + console.log(flattened(arr)); +})() From 228a5d30c10ddc724abfc7698fd3c57de08dfcd1 Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:35:13 +0530 Subject: [PATCH 2/6] Update FlattenedArray.js --- Recursive/FlattenedArray.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js index eb8560bac4..4e0ed1fc7c 100644 --- a/Recursive/FlattenedArray.js +++ b/Recursive/FlattenedArray.js @@ -1,5 +1,9 @@ + +// https://flexiple.com/flatten-array-javascript/ +// Flatten an given array to reduce the dimensionality of an array + const flattened = (arr) => { - const = [] + const = [] arr.forEach(i => { if(Array.isArray(i)){ res.push(...flattened(i)); From fadda859ffb1b5d8390be710ba92fe24b39a14c0 Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:41:56 +0530 Subject: [PATCH 3/6] Update FlattenedArray.js --- Recursive/FlattenedArray.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js index 4e0ed1fc7c..43fc0d476c 100644 --- a/Recursive/FlattenedArray.js +++ b/Recursive/FlattenedArray.js @@ -3,7 +3,7 @@ // Flatten an given array to reduce the dimensionality of an array const flattened = (arr) => { - const = [] + const res = [] arr.forEach(i => { if(Array.isArray(i)){ res.push(...flattened(i)); From d717a1db71f8f93a219ebdb54a08e35950f3ef76 Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Thu, 21 Oct 2021 12:03:31 +0530 Subject: [PATCH 4/6] Updated FlattenedArray.js --- Recursive/FlattenedArray.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js index 43fc0d476c..4bd4f65349 100644 --- a/Recursive/FlattenedArray.js +++ b/Recursive/FlattenedArray.js @@ -15,7 +15,4 @@ const flattened = (arr) => { return res; } -(() => { - const arr = [['a'],['bc'],[['cde'], ['f']]]; - console.log(flattened(arr)); -})() +module.exports = flattened; From 4ba6a1bbb55ffdd5f402da4d42dd8a4583b095ee Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Thu, 21 Oct 2021 15:14:39 +0530 Subject: [PATCH 5/6] Update FlattenedArray.js --- Recursive/FlattenedArray.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js index 4bd4f65349..5a555d3ebc 100644 --- a/Recursive/FlattenedArray.js +++ b/Recursive/FlattenedArray.js @@ -16,3 +16,16 @@ const flattened = (arr) => { } module.exports = flattened; + +// JEST TEST + +const flattened = require('./FlattenedArray'); + +test('Array', () => { + expect(flattened([['a'],['bc'],[['cde'], ['f']]])).toStrictEqual(['a','bc','cde','f']); +}); + + +test('Array', () => { + expect(flattened([[['a'],['bcd']],[['dcde'], ['fg']]])).toStrictEqual(['a','bcd','dcde','fg']); +}); From 32539af84eec595a8f88636f402c18b5000d0188 Mon Sep 17 00:00:00 2001 From: Velkumaran AP <38457682+vels10@users.noreply.github.com> Date: Thu, 21 Oct 2021 20:57:39 +0530 Subject: [PATCH 6/6] Update FlattenedArray.js --- Recursive/FlattenedArray.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Recursive/FlattenedArray.js b/Recursive/FlattenedArray.js index 5a555d3ebc..4bd4f65349 100644 --- a/Recursive/FlattenedArray.js +++ b/Recursive/FlattenedArray.js @@ -16,16 +16,3 @@ const flattened = (arr) => { } module.exports = flattened; - -// JEST TEST - -const flattened = require('./FlattenedArray'); - -test('Array', () => { - expect(flattened([['a'],['bc'],[['cde'], ['f']]])).toStrictEqual(['a','bc','cde','f']); -}); - - -test('Array', () => { - expect(flattened([[['a'],['bcd']],[['dcde'], ['fg']]])).toStrictEqual(['a','bcd','dcde','fg']); -});