-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.js
More file actions
20 lines (17 loc) · 690 Bytes
/
Q1.js
File metadata and controls
20 lines (17 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Wrt code input arr = [[1,2,3] , [4,5] , [2,2,3,4,[1,2,[3]]] , [0, [4,3]] , [2,2,3,4,3]] & o/p = sum of all array items
function sumNestedArray(arr) {
let total = 0;
for (let item of arr) {
if (Array.isArray(item)) {
total += sumNestedArray(item); // Recursive call for nested arrays
} else {
total += item; // Add the element to the total if it's not an array
}
}
return total;
}
// Given array
const arr = [[1, 2, 3], [4, 5], [2, 2, 3, 4, [1, 2, [3]]], [0, [4, 3]], [2, 2, 3, 4, 3]];
// Calculate and print the sum
const result = sumNestedArray(arr);
console.log("Sum of all array items:", result);