Shape Rule
Growing reverse
Each line adds one digit on the right of the partial reverse — 3 → 32 → 325 → 3256 → 32568.

Program 61 prints a growing reverse-number pattern: starting from an integer like 86523, build the reverse one digit at a time and print each partial result — 3, 32, 325, and so on. This tutorial covers modulo, reverse building, a live preview, worked JavaScript examples, edge cases, and O(d) complexity.
Growing reverse
Each line adds one digit on the right of the partial reverse — 3 → 32 → 325 → 3256 → 32568.
num !== 0
while (num !== 0) repeats once per digit until the source number is fully consumed.
num % 10
num % 10 extracts the last digit — 3 from 86523, then 2 from 8652, and so on.
reverse * 10 + digit
reverse = reverse * 10 + (num % 10) shifts left and appends the new digit on the right.
Any integer
Enter a starting number and watch the growing reverse pattern in the browser.
Complexity
One iteration per digit — 86523 produces five lines; total steps equal digit count.
A growing reverse number pattern builds the reversed form of an integer one digit at a time and prints each partial result. With num = 86523, you get 3, 32, 325, 3256, 32568.
In JavaScript use while (num !== 0), extract digits with num % 10, update reverse = reverse * 10 + digit, log with console.log(reverse), then num = Math.floor(num / 10).
Modulo plus reverse building is the standard technique for reversing numbers, checking palindromes, and digit-sum problems.
num % 10 — last digit each step.
reverse * 10 + digit appends on the right.
Program 60 shrinks the original; Program 61 grows the partial reverse.
Follow Program 60; continue to Program 62 next.
In short: while (num !== 0), reverse = reverse * 10 + num % 10, console.log(reverse), then num = Math.floor(num / 10).
Given starting integer num = 86523, build its reverse one digit at a time and print each partial reverse until all digits are processed.
// num = 86523
//3
//32
//325
//3256
//32568 | Item | Type | Description |
|---|---|---|
num | number | Source number — shrinks each iteration via Math.floor(num / 10). |
reverse | number | Partial reverse — starts at 0, grows each step. |
| Extract digit | number | num % 10 — last digit of current num. |
| Append digit | number | reverse = reverse * 10 + digit. |
| Log step | void | console.log(reverse) after each append. |
| Line count | number | Equals digit count of the starting number (86523 → 5 lines). |
reverse = 0
while num is not 0:
digit = num % 10
reverse = reverse * 10 + digit
log reverse
num = floor(num / 10) | Approach | Idea | Best for |
|---|---|---|
| Two-step append | reverse *= 10 then reverse += digit | Teaching each operation separately |
| Combined append | reverse = reverse * 10 + (num % 10) | Compact production code |
| Math.abs + parseInt(prompt()) | Negative handling and user input | User-input programs |
| Compact trace | num = 123 on paper first | Quick dry-runs |
| Arbitrary precision | JavaScript integers have no fixed limit | Very large starting values |
| Goal | Pattern |
|---|---|
| Loop | while (num !== 0) |
| Extract digit | digit = num % 10 |
| Append to reverse | reverse = reverse * 10 + digit |
| Log & shrink | console.log(reverse); num = Math.floor(num / 10) |
Same growing-reverse pattern — three ways to set the starting number and trace the logic.
let num = 86523;Hard-coded start for demos
parseInt(prompt())Read starting number from console
let num = 123;Quick dry-run on paper
num % 10Last digit each step
rev * 10 + dAppend digit on the right
Reach for this pattern when teaching modulo, reverse building, and while loops together in JavaScript.
Natural follow-up — same while loop, but build a growing reverse instead of shrinking the original.
Standard technique for reversing integers and checking palindromes.
Classic modulo + division question — explain extract-append-shrink before coding.
Compare single-number loops with 2D spiral grid filling.
JavaScript integers have arbitrary precision — no overflow for typical inputs.
Key benefit: one small program that locks in modulo, reverse building, and O(d) thinking.
Enter a positive integer between 10 and 99999999 and draw the growing reverse pattern in the browser.
Three complete JavaScript programs — fixed starting value, user input with abs, and a compact num = 123 trace. Click View Output to reveal sample console results.
Print the growing reverse pattern for a hard-coded starting integer with a while loop.
num = 86523Hard-coded start — build reverse one digit at a time and log after each append.
let num = 86523;
let reverse = 0;
while (num !== 0) {
reverse = reverse * 10;
reverse = reverse + (num % 10);
console.log(reverse);
num = Math.floor(num / 10);
} Extract 3 from 86523 → reverse becomes 3; then 2 → 32; then 5 → 325 — each line shows the partial reverse so far.
Read the starting number from the user and handle negatives with abs.
Read num with prompt(), validate with Number.isFinite, and use combined append in one expression.
const numInput = prompt("Enter a number:");
const parsed = parseInt(numInput, 10);
if (!Number.isFinite(parsed)) {
console.log("Please enter a valid integer.");
} else {
let num = Math.abs(parsed);
let reverse = 0;
while (num !== 0) {
reverse = reverse * 10 + (num % 10);
console.log(reverse);
num = Math.floor(num / 10);
}
} Same loop core as Example 1; combines multiply-and-add into one line and uses abs for negatives.
Use num = 123 for a quick paper trace before larger demos.
num = 123Same while loop with a smaller starting value — easy to dry-run on paper.
let num = 123;
let reverse = 0;
while (num !== 0) {
reverse = reverse * 10 + (num % 10);
console.log(reverse);
num = Math.floor(num / 10);
} Three iterations: extract 3 → reverse 3; extract 2 → reverse 32; extract 1 → reverse 321.
num is the source number; reverse starts at 0.
num % 10 gives the current last digit (3, then 2, then 5, …).
reverse = reverse * 10 + digit shifts existing digits left and appends the new one.
console.log(reverse) then num = Math.floor(num / 10) moves to the next digit.
One line per digit processed — O(d) time for d digits, O(1) extra memory.
num = 86523Trace each loop iteration: extract the digit, update reverse, log, then shrink num.
| Step | num | Digit num % 10 | reverse after append | Logged |
|---|---|---|---|---|
| 1 | 86523 | 3 | 3 | 3 |
| 2 | 8652 | 2 | 32 | 32 |
| 3 | 865 | 5 | 325 | 325 |
| 4 | 86 | 6 | 3256 | 3256 |
| 5 | 8 | 8 | 32568 | 32568 |
Total lines logged: 5 = digit count of the starting number. Final reverse equals the full reversed number.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
num % 10 extracts digits — concrete introduction to the remainder operator.
Example: trace num = 123 and watch reverse grow 3, 32, 321.
Standard building block for reversing integers and palindrome checks.
Example: stop after the loop and compare final reverse to the original.
Program 60 shrinks the original; Program 61 grows the partial reverse — same loop, different output.
Example: log both patterns side by side for 86523.
Move from single-number loops to 2D spiral grid filling next.
Example: compare while-loop digit work with nested boundary loops.
One iteration per digit — makes O(d) concrete for beginners.
Example: num = 1000000 produces seven lines — seven digits.
Pair the pattern with parseInt(prompt()) and input validation.
Example: use long when reversed values may exceed int range.
Pro Tip: when an interviewer asks for reverse building, explain extract-append-shrink before writing the loop.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong append order or missing shrink shows up immediately as broken output.
Modulo and integer division together — essential digit-manipulation toolkit.
Print both num and reverse, collect steps in a list, or check palindromes with small edits.
Only num and reverse needed — no arrays required.
Pro Tip: trace num and reverse on paper for 123 before coding — watch reverse grow 3, 32, 321.
Small habits that keep growing-reverse code clean.
reverse = 0 before the loop — first digit becomes the first logged value.
Use Number.isFinite after parseInt(prompt()) for user input.
reverse * 10 + digit — multiply shifts left, then append the new digit.
num = Math.floor(num / 10) after logging — without it the loop never advances.
Trace num = 123 on paper before coding larger demos.
Pro Tip: if reverse stays at single digits, you probably forgot to multiply by 10 before adding.
Mistakes that commonly break growing-reverse patterns.
Adding digits without shifting leaves reverse as single digits — 3, 2, 5 instead of 3, 32, 325.
→ Always do reverse = reverse * 10 + digit.
Without num = Math.floor(num / 10), the same digit is extracted forever — infinite loop.
→ Divide by 10 with Math.floor after each append and log.
num / 10 returns a float in JavaScript — the loop may behave unexpectedly.
→ Use num = Math.floor(num / 10) for integer digit removal.
Letters or empty input return NaN when parseInt(prompt()) is unchecked.
→ Validate with Number.isFinite and re-prompt on failure.
Float division on digits introduces precision errors — stick to integer operations.
→ Keep num and reverse as integer types.
Check these inputs before calling the solution done.
while (num !== 0) never runs — log nothing or show a message.
One line prints 7 — simplest case.
Builds 0, 2, 21 — zero is extracted first from the right.
Apply Math.abs(num) before the loop — see Example 2.
Bare parseInt(prompt()) returns NaN — validate with Number.isFinite.
JavaScript integers have arbitrary precision — no overflow for typical inputs.
Try these variations to lock in the pattern.
num = 123 before codingreverse * 10 shifts existing digits left before the new digit is appended on the right.long when reversed values may exceed int.MaxValue.reverse after the loop equals the fully reversed number — 32568 for input 86523.Quick Takeaway: while (num !== 0), reverse = reverse * 10 + num % 10, log, then num = Math.floor(num / 10).
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) — d = digit count | O(1) |
| Compact trace (Example 3) | O(d) | O(1) |
The growing reverse number pattern is a small while-loop exercise with lasting payoff: modulo, reverse building, and O(d) intuition. Master the fixed-value version, then try user input with long and the compact num = 123 trace.
Practice the three examples above, then continue to Program 62 for the next pattern in the series.
Extract with modulo, append with multiply-and-add, log reverse, then shrink num — validate input when reading from prompt().
reverse = 0 before the loopreverse = reverse * 10 + (num % 10)Number.isFinite after parseInt(prompt()) for user inputnum = Math.floor(num / 10) inside the loop/ without Math.floor for digit removalnum = 123 dry-run before larger demosPrint the growing reverse pattern the beginner-friendly way.
Build reverse digit by digit
Definitionnum % 10 extracts digit
Mathreverse * 10 + digit
CodeMath.floor(num / 10) each step
LoopO(d) time
AnalysisEach 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 num = Math.floor(num / 10) — runtime is O(d) for d digits.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful