LeetCode: Add Digits Solution

Could work with string instead of using math

Implementation

1var addDigits = function (num) {
2 num = String(num)
3
4 while (num.length > 1) {
5 num = String(num.split("").reduce((acc, el) => acc + +el, 0))
6 }
7
8 return +num
9}

References

Original problem

Similar problems

Happy Number

Sum of Digits in the Minimum Number

Sum of Digits of String After Convert

Minimum Sum of Four Digit Number After Splitting Digits

Calculate Digit Sum of a String

Comments

Loading comments...

Tags

leetcode

math

string

Next Post

LeetCode: Sum of Digits of String After Convert

Solving a problem in one go is such a good feeling

Previous Post

HoningJS

Search Posts