A growing reverse-number pattern builds the reverse of an integer one digit at a time — logging the partial reverse after each append — so digits appear from right to left of the original.
Follows the remove-last-digit pattern in Program 60; next is the spiral matrix in Program 62.
Approach
How to Solve It
Peel digits from the right with %, append them into reverse, log after each append, then shrink num with Math.floor(num / 10).
Method
Idea
Best for
while + modulo build
reverse = reverse * 10 + (num % 10), then log
Learning, interviews, digit drills
vs Program 60
60 shrinks num; 61 grows a separate reverse
Comparing digit techniques
Pseudocode
Pseudocode
reverse = 0
while num !== 0:
reverse = reverse * 10 + (num % 10)
print reverse
num = floor(num / 10)
Cheat sheet
Goal
Pattern
Loop
while (num !== 0)
Last digit
num % 10
Append digit
reverse = reverse * 10 + (num % 10);
Print partial
console.log(reverse);
Next digit
num = Math.floor(num / 10);
Negatives
num = Math.abs(parsed); before the loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Not needed here — each step is its own line
console.log(reverse)
Ends the current row
Every partial reverse
Same idea as C# WriteLine: each call prints a full value and moves to the next line.
Try it
Live Preview
Change the starting number and the growing reverse pattern updates instantly — absolute value, up to 9 digits.
Whole numbers from 1 to 999999999 (negatives use absolute value). Tap a chip or type a value.
Live resultnum = 86523 · 5 lines
3
32
325
3256
32568
Trace
Worked Walkthrough
Trace num = 86523 — append each rightmost digit into reverse, then log.
Step
num % 10
reverse after append
1
3
3
2
2
32
3
5
325
4
6
3256
5
8
32568
Final line 32568 is the full reverse of 86523. Five digits → five lines → O(d).
Code
JavaScript Programs
Three complete programs: fixed 86523, prompt input with Math.abs, and a compact 123 demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed num = 86523
Append each rightmost digit into reverse and log after every step.
JavaScript
let num = 86523;
let reverse = 0;
while (num !== 0) {
reverse = reverse * 10 + (num % 10);
console.log(reverse);
num = Math.floor(num / 10);
}
1. Three digits. Peel 3, then 2, then 1 → 3, 32, 321.
2. Trace on paper. Confirm you log after appending — that is what grows each line.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for 86523 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
print before append
Stale reverse values
If you log before updating reverse, the first line is 0. Append, then log.
forget * 10
Digits overwrite
Without reverse * 10, you only keep the latest digit — never a growing number.
trailing 0
Leading zero line
120 prints 0 first because the rightmost digit is zero — expected, not a bug.
num = 0
Empty output
while (num !== 0) skips entirely when the start is already 0.
Integer divide
Use Math.floor, not plain /
Plain division keeps decimals and never reaches exact 0 the same way.
Bad input
Validate parseInt
Check Number.isFinite(parsed) before the loop; use Math.abs for negatives.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(d)
O(1)
User input (Example 2)
O(d)
O(1)
Each iteration handles one digit — for d digits the loop runs d times.
Remember
Key Takeaways
Append then print:reverse = reverse * 10 + (num % 10), then console.log.
Modulo peels right:% 10 reads the next digit; Math.floor(/ 10) advances.
vs Program 60: 60 shrinks the original; 61 grows a separate reverse.
Complexity:O(d) time for d digits; O(1) extra space.
One line:reverse = reverse * 10 + num % 10, log, then num = Math.floor(num / 10).
Frequently Asked Questions
It prints a growing reverse-number pattern like 3, 32, 325, 3256, 32568 when starting from 86523.
It takes the last digit using num % 10 and appends it with reverse = reverse * 10 + digit, then logs reverse.
num = Math.floor(num / 10) removes the last digit so the loop can move to the next digit from the right.
Program 60 logs the shrinking original number. Program 61 builds and logs a growing partial reverse using modulo and multiplication.
Program 61 uses a single while loop on one number. Program 62 fills an n×n spiral grid with a 2D array.
Multiplying shifts existing digits left — reverse * 10 + digit appends the new digit on the right.
If the source ends in 0, that digit is extracted first — e.g. 120 gives 0, then 2, then 21.
Apply Math.abs(num) before the loop — see Example 2.
O(d) where d is the number of digits — the loop runs once per digit.
while (num !== 0) skips the body — nothing is printed.
🤔
Did you know?
Each iteration takes the last digit with num % 10, appends it to reverse via reverse = reverse * 10 + digit, logs the partial reverse, then shrinks num with Math.floor(num / 10) — runtime is O(d) for d digits.