Sum of Digits Recursive
Oracle's data analytics platforms frequently process large volumes of numerical data, often requiring specific transformations for validation, aggregation, or feature engineering. One fundamental operation is computing the sum of digits of an integer. This problem asks you to implement a recursive solution for this task, emphasizing efficient and elegant code that aligns with best practices for handling numerical data.
Your task is to implement a recursive function, sumDigits, that accepts a non-negative integer n and returns the sum of its individual digits.
n: A non-negative integer (number type in TypeScript/JavaScript).n (number type).n is a single-digit number? The function should return n itself.n is 0? The function should return 0.n will always be non-negative as per constraints.Examples
Example 1
Input:
n = 123
Output:
6
Note:
1 + 2 + 3 = 6
Example 2
Input:
n = 45
Output:
9
Note:
4 + 5 = 9
Example 3
Input:
n = 0
Output:
0
Note:
The sum of digits for 0 is 0.
Example 4
Input:
n = 907
Output:
16
Note:
9 + 0 + 7 = 16
Constraints
0 <= n <= 10^9n is an integer.n). Assume valid input per constraints.number type precision (e.g., using BigInt in JavaScript).Loading editor...