Skip to content

Instantly share code, notes, and snippets.

@johnstew
Created April 3, 2013 14:32
Show Gist options
  • Save johnstew/5301736 to your computer and use it in GitHub Desktop.
Save johnstew/5301736 to your computer and use it in GitHub Desktop.
JS Digital Root
function root(num){
var total = 0;
if(num.toString().length == 1){
var iNum = parseInt(num);
return iNum;
}else{
num.toString().split("").forEach( function(value){
var iValue = parseInt(value);
return total += iValue;
});
return root(total);
}
}
@br0wsa
Copy link

br0wsa commented Oct 26, 2022

function digitalRoot(n) {
return n % 9;
}
that's it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment