Skip to content

Commit 7e97074

Browse files
Merge pull request #92 from wakidurrahman/feat/feat/code-challenges-004
feat: Sum of several arrays
2 parents 38caeb1 + e38e0b9 commit 7e97074

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/code-challenges/code-challenges-003-010.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,37 @@ function simpleClockAngle(number) {
1212
// gives us the number of degrees for one minute
1313
return 6 * number;
1414
}
15+
16+
/**
17+
* Q004: Sum of several arrays
18+
* Problem
19+
* 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.
20+
* 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.
21+
*/
22+
23+
// Normal
24+
function multiDimensionalSumOfSeveralArrayVariationNormal(array) {
25+
// Store our final answer
26+
let sum = 0;
27+
// Loop through entire array.
28+
for (let i = 0; i < array.length; i++) {
29+
// Loop through each inner array.
30+
for (let j = 0; j < array[i].length; j++) {
31+
sum += array[i][j];
32+
}
33+
}
34+
35+
return sum;
36+
}
37+
38+
multiDimensionalSumOfSeveralArrayVariationNormal([[3, 2], [1], [4, 12]]);
39+
// reduce
40+
41+
function multiDimensionalSumOfSeveralArrayVariationReduce(array) {
42+
return array.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e);
43+
}
44+
multiDimensionalSumOfSeveralArrayVariationReduce([[3, 2], [1], [4, 12]]);
45+
46+
/**
47+
* Q005: Test divisors of three
48+
*/

0 commit comments

Comments
 (0)