Digit Sum
MATH
Problem
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit, and return it.
Examples
digitSum(99) // returns 9 // 99 becomes 9 + 9 which equals 18 // 18 then reduces to 1 + 8 which is 9 digitSum(10) // returns 1 // 10 is 1 + 0, resulting in 1. digitSum(12345) // returns 6 // 12345 becomes 1 + 2 + 3 + 4 + 5, totaling 15 // 15 then simplifies to 1 + 5, giving 6
Loading...