-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeepSum.js
62 lines (59 loc) · 911 Bytes
/
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
function deepSum (arr){
var temp = 0;
if (arr.length === 0){
return 'No number';
}
for (var i in arr){
for (var j in arr[i]){
for (var k in arr[i][j]){
temp += arr[i][j][k];
}
}
}
// 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++){
// temp += arr[i][j][k];
// }
// }
// }
return temp;
}
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