I have created a function that aims to calculate the digital root of a given number.
Despite my efforts, I am encountering an issue where this function consistently returns undefined
, even though the variable does hold the correct result.
Can you help me identify where I might be going wrong?
function findDigitalRoot(number: number): number {
let sum: number = 0;
while (number > 0) {
sum += number % 10
number = Math.floor(number / 10)
}
if (sum >= 10) {
findDigitalRoot(sum)
} else {
alert(sum) // sum=6
return sum // undefined
}
}
alert(findDigitalRoot(942)) // undefined