DEV Community

duccanhole
duccanhole

Posted on

code everyday with me

--DAY 26--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
Problem: Add Digits
Detail: here
My solution (javascript):

var addDigits = function(num) {
    if(num<=9) return num;
    let n=0;
    while(num>0){
        n+=num%10;
        num=Math.floor(num/10);
    }
    return addDigits(n);
};
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

Top comments (1)

Collapse
 
sherifhegazy profile image
sherifhegazy
  m=   num.toString().split('').reduce((prev,post)=>+prev + +post,0)

while (m>9){
    m=m.toString().split('').reduce((prev,post)=>+prev + +post,0)

}
Enter fullscreen mode Exit fullscreen mode

return m