Leetcode In JS #258 Add Digits

Problem Description

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number} num
* @return {number}
* @desc trival solution including recursion.
*/
var addDigits = function (num) {
var quotient = num,
remainder = 0,
sum = 0;

while (quotient >= 10) {
remainder = quotient % 10;
sum = sum + remainder;
quotient = parseInt(quotient / 10);
}

sum = sum + quotient;

if (sum >= 10) {
return addDigits(sum);
} else {
return sum;
}
};