← Back to challenges

Sum of Digits of a Positive Integer

JavaScriptHardrecursionalgorithmsbugs

Instructions

An instructor has given her student an assignment to calculate the sum of the digits of a positive integer using recursion.The student came up with the solution but the solution does not seem to be correct. Help her fix the error.

Examples

sumDigit(111) ➞ 3
// 1 + 1 + 1 = 3

sumDigit(222) ➞ 6
// 2 + 2 + 2 = 6

sumDigit(333) ➞ 9
// 3 + 3 + 3 = 9

Notes

Input is a positive integer.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Alphanumeric Restriction