Digit Degree

Description


Let’s define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

  • For n = 5, the output should be digitDegree(n) = 0;
  • For n = 100, the output should be digitDegree(n) = 1. 1 + 0 + 0 = 1.
  • For n = 91, the output should be digitDegree(n) = 2. 9 + 1 = 10 -> 1 + 0 = 1.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

Guaranteed constraints: 5 \leq n \leq 10^9.

  • [output] integer

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
4
5
function digitDegree(n) {
    if (n<10)
        return 0;
    return 1 + digitDegree(Number(String(n).split('').map(Number).reduce((a,b)=>a+b)));
}