Generation

generate functionThu, 24 Nov 2022

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.

function digitDegree(n: number): number { let current = n; let count = 0; if (n <= 9) { return count; } while (current > 9) { count++; current = getDigit(current); } return count; } function getDigit(num: number): number { let arr = num.toString().split(''); let sum = 0; for (let i = 0; i < arr.length; i++) { sum += Number(arr[i]); } return sum; }

TypeScript
Generate More

Questions about programming?Chat with your personal AI assistant