-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp0-w4-e13-deepSum.js
81 lines (74 loc) · 1.23 KB
/
p0-w4-e13-deepSum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Week-4 Exercise 13 Deep Sum
/*
Implementasikan function deepSum untuk mendapatkan jumlah pertambahan dari angka-angka
yang terdapat di dalam array
Contoh array:
[
[
[1, 2, 3],
[1, 2, 3]
],
[
[1, 2, 3],
[1, 2],
[1]
]
]
Maka akan menghasilkan output: 22
Level kedalaman array akan di-asumsikan selalu 3, [ [ [ ] ] ], jika yang diberikan adalah array kosong
maka kembalikan nilai 'No number'
*/
function deepSum(arr) {
var jml = 0;
for(var i=0; i<arr.length; i++) {
for(var j=0; j<arr[i].length; j++) {
for(var k=0; k<arr[i][j].length; k++) {
jml += arr[i][j][k]
}
}
}
if(arr.length===0) {
return 'No Number';
}
else {
return jml;
}
}
console.log(deepSum([
[
[4, 5, 6],
[9, 1, 2, 10],
[9, 4, 3]
],
[
[4, 14, 31],
[9, 10, 18, 12, 20],
[1, 4, 90]
],
[
[2, 5, 10],
[3, 4, 5],
[2, 4, 5, 10]
]
])); // 316
console.log(deepSum([
[
[20, 10],
[15],
[1, 1]
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[2],
[9, 11]
],
[
[3, 5, 1],
[1, 5, 3],
[1]
],
[
[2]
]
])); // 156
console.log(deepSum([])); // No number