From 64e11a1ffec29a5cdcb9b3a29aacbd886e390599 Mon Sep 17 00:00:00 2001 From: wakidurrahman Date: Thu, 15 Jun 2023 18:01:27 +0900 Subject: [PATCH 1/2] feat: Sum of several arrays --- .../code-challenges-003-010.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/code-challenges/code-challenges-003-010.js b/src/code-challenges/code-challenges-003-010.js index ba481a4..0161f34 100644 --- a/src/code-challenges/code-challenges-003-010.js +++ b/src/code-challenges/code-challenges-003-010.js @@ -12,3 +12,33 @@ function simpleClockAngle(number) { // gives us the number of degrees for one minute return 6 * number; } + +/** + * Q004: Sum of several arrays + * Problem + * You will be given an array of several arrays that each contain integers and your goal is to write a function that will sum up all the numbers in all the arrays. + * For example, if the input is [[3, 2], [1], [4, 12]] then your program should output 22 because 3 + 2 + 1 + 4 + 12 = 22. Solve without and with reduce. + */ + +// Normal +function multiDimensionalSumOfSeveralArrayVariationNormal(array) { + // Store our final answer + let sum = 0; + // Loop through entire array. + for (let i = 0; i < array.length; i++) { + // Loop through each inner array. + for (let j = 0; j < array[i].length; j++) { + sum += array[i][j]; + } + } + + return sum; +} + +multiDimensionalSumOfSeveralArrayVariationNormal([[3, 2], [1], [4, 12]]); +// reduce + +function multiDimensionalSumOfSeveralArrayVariationReduce(array) { + return array.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e); +} +multiDimensionalSumOfSeveralArrayVariationReduce([[3, 2], [1], [4, 12]]); From e38e0b9b9e16d32c49c67fb734741c04eb20782e Mon Sep 17 00:00:00 2001 From: wakidurrahman Date: Fri, 16 Jun 2023 16:53:45 +0900 Subject: [PATCH 2/2] feat: added --- src/code-challenges/code-challenges-003-010.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/code-challenges/code-challenges-003-010.js b/src/code-challenges/code-challenges-003-010.js index 0161f34..bcc82ed 100644 --- a/src/code-challenges/code-challenges-003-010.js +++ b/src/code-challenges/code-challenges-003-010.js @@ -42,3 +42,7 @@ function multiDimensionalSumOfSeveralArrayVariationReduce(array) { return array.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e); } multiDimensionalSumOfSeveralArrayVariationReduce([[3, 2], [1], [4, 12]]); + +/** + * Q005: Test divisors of three + */