To condense a number means repeatedly add its digits until only one digit remains. That final digit is the digital root.
Remember
while n has more than one digit:
n = sum of digits of n
Example: 9875
9 + 8 + 7 + 5 = 29
2 + 9 = 11
1 + 1 = 2 ← condensed result
n
Reduction chain
Result
38
38 → 11 → 2
2
999
999 → 27 → 9
9
7
(already one digit)
7
Approach
How to Condense a Number in JavaScript
Four clear steps — then pick an iterative loop or the O(1) formula.
Work with a nonnegative value (use Math.abs if needed).
While the value is greater than 9, replace it with the sum of its digits.
Stop when only one digit remains.
Or skip the loops and compute the digital root with n % 9.
Method
Idea
Time
Best for
Digit-sum loop
Sum digits until n <= 9
O(log n)
Interviews, showing the process
Modulo 9 formula
n === 0 ? 0 : 1 + (n - 1) % 9
O(1)
Production code, quick answers
Pseudocode
Pseudocode
function Condense(n):
n = abs(n)
while n > 9:
sum = 0
while n > 0:
sum = sum + (n mod 10)
n = n / 10
n = sum
return n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Goal
Reduce to a single digit 0–9
Stop when
n <= 9
One digit already
Return n unchanged
Fast formula
n === 0 ? 0 : 1 + (n - 1) % 9
Multiples of 9
For n > 0, digital root is 9 (not 0)
Classic check
9875 → 2
Try it
Live Preview
Enter any integer to see the reduction chain and the condensed digit.
Whole numbers (absolute value used). Tap a chip or type a value.
Result
n = 9875
Chain: 9875 → 29 → 11 → 2
Condensed = 2
Code
JavaScript Programs
Start with the iterative digit-sum loop so you can show each reduction. Then use the O(1) modulo formula when speed matters.
Example 1 — Digit-Sum Loop
Keep replacing the number with the sum of its digits until it is a single digit.
JavaScript
function condense(n) {
n = Math.abs(n);
while (n > 9) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
n = sum;
}
return n;
}
const number = 9875;
console.log(number + " condensed to " + condense(number));