Add Digits
Description
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input:38Output: 2 Explanation: The process is like:3 + 8 = 11,1 + 1 = 2. Since2has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Solution(javascript)
/**
 * @param {number} num
 * @return {number}
 */
var addDigits = function(num) {
    function cal(num){
        var s = num.toString();
        var digits = s.split("").map(Number);
        if(digits.length === 1){
            return digits[0];
        }
        num = digits.reduce(function(pre, cur){
            return pre + cur;
        });
        return cal(num);
    }
    
    return cal(num);
};