JavaScript Number Pattern (Progressive Reverse)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

Remember
Rule: digit = num % 10
      reverse = reverse * 10 + digit
      print reverse; num = floor(num / 10)

3
32
325
3256
32568     ← start = 86523

Follows the remove-last-digit pattern in Program 60; next is the spiral matrix in Program 62.

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).

MethodIdeaBest for
while + modulo buildreverse = reverse * 10 + (num % 10), then logLearning, interviews, digit drills
vs Program 6060 shrinks num; 61 grows a separate reverseComparing digit techniques

Pseudocode

Pseudocode
reverse = 0
while num !== 0:
    reverse = reverse * 10 + (num % 10)
    print reverse
    num = floor(num / 10)

Cheat sheet

GoalPattern
Loopwhile (num !== 0)
Last digitnum % 10
Append digitreverse = reverse * 10 + (num % 10);
Print partialconsole.log(reverse);
Next digitnum = Math.floor(num / 10);
Negativesnum = Math.abs(parsed); before the loop

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowNot needed here — each step is its own line
console.log(reverse)Ends the current rowEvery partial reverse

Same idea as C# WriteLine: each call prints a full value and moves to the next line.

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 result num = 86523 · 5 lines
3
32
325
3256
32568

Worked Walkthrough

Trace num = 86523 — append each rightmost digit into reverse, then log.

Stepnum % 10reverse after append
133
2232
35325
463256
5832568

Final line 32568 is the full reverse of 86523. Five digits → five lines → O(d).

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);
}
Try It Yourself

How It Works

1. Peel. num % 10 reads the rightmost digit (3 from 86523).

2. Append. reverse * 10 + digit shifts left and adds the new digit on the right.

3. Shrink. Math.floor(num / 10) drops that digit so the next iteration uses the next one.

Example 2 — User Input Number

Read with prompt, validate, use Math.abs, then run the same append-and-log loop.

JavaScript
const parsed = parseInt(prompt("Enter a number:"), 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);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Reject non-finite parses before entering the loop.

2. Absolutize. Math.abs keeps digit math positive for negative input.

3. Trailing zero demo. 120 peels 0 first → lines 0, 2, 21.

Example 3 — Compact num = 123

Three lines only — easy to dry-run on paper before larger demos.

JavaScript
let num = 123;
let reverse = 0;

while (num !== 0) {
  reverse = reverse * 10 + (num % 10);
  console.log(reverse);
  num = Math.floor(num / 10);
}
Try It Yourself

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Perfect Square Spiral

Continue with a 2D spiral that fills an n×n grid with consecutive numbers.

Program 62 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful