JavaScript: Add digits until there is only one digit
JavaScript Math: Exercise-99 with Solution
Sum Digits Until One Digit
Write a JavaScript program to add repeatedly all the digits of a given non-negative number until the result has only one digit.
For example:
Input: 39
Output: 3
Explanation: The formula is like: 3 + 9 = 12, 1 + 2 = 3.
Visual Presentation:

Test Data:
(47) -> 2
(9) -> 9
Sample Solution-1:
JavaScript Code:
Output:
n = 49 Add digits of the said number until there is only one digit: 4 Original text: 9 Add digits of the said number until there is only one digit: 9
Flowchart:

Live Demo:
Sample Solution-2:
JavaScript Code:
Output:
n = 49 Add digits of the said number until there is only one digit: 4 Original text: 9 Add digits of the said number until there is only one digit: 9
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that repeatedly sums the digits of a number until a single digit remains, returning that digit.
- Write a JavaScript function that implements the digital root algorithm using a loop.
- Write a JavaScript function that uses recursion to reduce a number to its digital root.
- Write a JavaScript function that validates non-negative input before performing repeated digit summation.
Improve this sample solution and post your code through Disqus.
Previous: Column number that relates to a column title.
Next: Check a given number is an ugly number or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.