Skip to content

Instantly share code, notes, and snippets.

@aire-con-gas
Created March 17, 2020 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aire-con-gas/bf3ef71cc0d954d35f2fc87f409f0f54 to your computer and use it in GitHub Desktop.
Save aire-con-gas/bf3ef71cc0d954d35f2fc87f409f0f54 to your computer and use it in GitHub Desktop.
function digital_root(n) {
// ...
function sum_roots(m) {
let rootArr = [];
let currentNum = m;
do {
rootArr.push(currentNum % 10);
currentNum = Math.trunc(currentNum / 10);
} while (currentNum >= 1)
return rootArr.reduce((acc, val) => acc + val, 0);
}
let result = n;
while(result > 10) {
result = sum_roots(result);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment