Created
September 19, 2020 07:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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