Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 07:52
Show Gist options
  • Save hassan-maavan/3270da769552c38819e3dbfbf815a927 to your computer and use it in GitHub Desktop.
Save hassan-maavan/3270da769552c38819e3dbfbf815a927 to your computer and use it in GitHub Desktop.
Digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers. https://www.codewars.com/kata/541c8630095125aba6000c00
function digital_root(n) {
let values = convertIntToArray(n);
let sum = 0;
sum = values.reduce(doSum);
if((convertIntToArray(n)).length > 1) {
return digital_root(sum);
} else {
return sum;
}
}
function convertIntToArray(int){
return Array.from(String(int), Number);
}
function doSum(total, num){
return (total + num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment