Sum of Deep Event Values
Clevertap helps businesses understand user behavior by collecting and analyzing event data. For advanced analytics and reporting, event metrics are sometimes aggregated into hierarchical structures, representing different segments or user journeys. To get a complete picture, we often need to sum up these values across all levels of nesting.
Your task is to implement a function sumDeepValues that takes a nested array. This array can contain either numbers or other nested arrays (which themselves can contain numbers or further nested arrays). The function should calculate and return the total sum of all numbers present at any level within this nested structure.
nestedArray: An array ((number | number[])[]) where each element is either a number or another array. The nested arrays can contain further numbers or nested arrays.number representing the sum of all numbers found in the nestedArray at any level.nestedArray will only contain numbers or arrays. You don't need to handle other data types like strings, booleans, or objects.0 to the total sum.Examples
Example 1
Input:
nestedArray = [1, 2, 3]
Output:
6
Note:
1 + 2 + 3 = 6
Example 2
Input:
nestedArray = [1, [2, 3], 4]
Output:
10
Note:
1 + (2 + 3) + 4 = 10
Example 3
Input:
nestedArray = [1, [2, [3, []], 4], 5, [-1]]
Output:
14
Note:
1 + (2 + (3 + 0) + 4) + 5 + (-1) = 1 + 9 + 5 - 1 = 14
Constraints
Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER).nestedArray itself can be empty.Loading editor...