LeetCode: Add Digits Solution
Could work with string instead of using mathImplementation
1var addDigits = function (num) {2 num = String(num)34 while (num.length > 1) {5 num = String(num.split("").reduce((acc, el) => acc + +el, 0))6 }78 return +num9}
References
Similar problems
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
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Sum of Digits of String After Convert
Solving a problem in one go is such a good feeling
Previous Post
CodeWars: Sudoku Solution Validator
Three checks