Q:

Write a JavaScript program to find the number of times to replace a given number with the sum of its digits until it convert to a single digit number

0

Write a JavaScript program to find the number of times to replace a given number with the sum of its digits until it convert to a single digit number. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function digit_to_one(num) {

    var digitSum = function(num) {

        var digit_sum = 0;
        while (num) {
            digit_sum += num % 10;
            num = Math.floor(num / 10);
        }

        return digit_sum;
    };

    var result = 0;

    while (num >= 10) {
        result += 1;
        num = digitSum(num);
    }

    return result;
}

console.log(digit_to_one(123))
console.log(digit_to_one(156))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now