What digit math is
Plenty of interview warm-ups hand you nothing but a whole number: reverse it, is it a palindrome?, sum its digits. No array, no map — so what do you even loop over?
The answer is two tiny moves that work on any whole number:
n % 10— the remainder after dividing by 10. Since our numbers are written in tens, the remainder is exactly the last digit:472 % 10is2.Math.floor(n / 10)— divide by 10 and drop the fraction. That chops the last digit off:472becomes47.
Run both in a loop and the number reads itself out to you, right to left:
472 → 47 → 4 → 0. When it reaches 0, every digit has been visited. Two operators, and the whole family of digit problems opens up.console.log(472 % 10); // 2
console.log(Math.floor(472 / 10)); // 47How it works, step by step
Let's use the two moves to reverse a number:
472 should become 274. Keep two variables: num (what is left to read) and rev (the answer being built). Each turn, peel the last digit off num and append it to rev with rev = rev * 10 + digit — multiplying by 10 shifts rev left one place, making room at the end.Here is every value, turn by turn:
| turn | num | digit = num % 10 | rev = rev × 10 + digit | num after ÷10 |
|---|---|---|---|---|
| 1 | 472 | 2 | 0 × 10 + 2 = 2 | 47 |
| 2 | 47 | 7 | 2 × 10 + 7 = 27 | 4 |
| 3 | 4 | 4 | 27 × 10 + 4 = 274 | 0 |
num hit 0, the loop stops, and rev holds 274. The digits came out right-to-left and were appended left-to-right — that crossing is what reverses them.Watch it run in the animation — each green digit has been folded into
rev, and the variable strip tracks num, digit, and rev every step. Try 1200 and watch the trailing zeros quietly disappear.This section has a step-by-step animation in the interactive lesson.
function reverseDigits(n) {
let num = n;
let rev = 0;
while (num > 0) {
const digit = num % 10;
rev = rev * 10 + digit;
num = Math.floor(num / 10);
}
return rev;
}
console.log(reverseDigits(472)); // 274
console.log(reverseDigits(1200)); // 21Quick check
Reversing 1200 gives 21, not 0021. Why?
Answer it in the interactive lesson to keep your progress.
Where it breaks
Three traps catch nearly everyone the first time:
- Plain
/keeps the fraction. In JavaScript47 / 10is4.7, not4. ForgetMath.floorandnumnever reaches0cleanly — the loop spins on values like4.7,0.47, … Always floor. - Negative numbers keep their sign.
-47 % 10is-7in JavaScript. If negatives are allowed, deal with the sign first (if (n < 0) …) instead of hoping the loop copes. - Overflow in other languages. Reversing
1534236469gives9646324351, which does not fit a 32-bit integer. JavaScript numbers survive this, but interviewers love asking "what if this were a 32-bit int?" — mention the check and you look prepared.
console.log(-47 % 10); // -7
console.log(47 / 10); // 4.7
console.log(Math.floor(47 / 10)); // 4Quick check
You wrote num = num / 10 instead of Math.floor(num / 10). What happens with num = 47?
Answer it in the interactive lesson to keep your progress.
How fast is it? (complexity)
One loop turn peels one digit, so the work is O(d) where
d is the number of digits. A number n has about log₁₀ n digits — a billion is only 10 digits, 10 turns. Digit loops are effectively instant.| Approach | Time | Space |
|---|---|---|
| Convert to string, reverse, convert back | O(d) | O(d) |
% 10 + Math.floor(/ 10) arithmetic | O(d) | O(1) |
Key takeaways:
n % 10reads the last digit;Math.floor(n / 10)removes it.rev = rev * 10 + digitappends a digit — the shift-left-then-add move.- Digit loops run once per digit:
O(log₁₀ n), effectively constant in practice. - Handle negatives and mention 32-bit overflow before the interviewer asks.
The same two moves power a family of classics you'll meet later: palindrome number (reverse and compare — this topic's Your Turn), Euclid's GCD (shrink a pair with remainders — watch it in the See it step, and use it in the Level-2 challenge), and count primes with a sieve. Digit math also returns inside hashing and bit manipulation, where
% 2 and / 2 do for binary what % 10 and / 10 do for decimal.