JavaScript Number Pattern (Remove Last Digit)

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

What Is This Pattern?

A remove-last-digit pattern prints an integer, then drops its rightmost digit with Math.floor(num / 10), repeating until the value becomes 0.

Remember
Rule: print, then num = Math.floor(num / 10)

86523
8652
865
86
8   ← start = 86523

Unlike Program 59 (grid border loops), this uses one while loop — line count equals the digit count.

How to Solve It

Loop while num !== 0: log the current value, then integer-divide by 10.

MethodIdeaBest for
While + dividePrint, then Math.floor(num / 10)Classic digit-removal demos
CompactSame loop with a smaller start (e.g. 123)Quick dry-runs

Pseudocode

Pseudocode
while num !== 0:
    print num
    num = floor(num / 10)

Cheat sheet

GoalPattern
Loopwhile (num !== 0)
Print currentconsole.log(num);
Drop last digitnum = Math.floor(num / 10);
Negativesnum = Math.abs(parsed); before the loop
Lines printedDigit count of the start value

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(num)Ends the current rowEvery shrinking value

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 shrinking sequence updates instantly.

Positive integers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.

Live result start = 86523 · 5 lines
86523
8652
865
86
8

Worked Walkthrough

Trace num = 86523 — print first, then divide.

StepPrintedAfter Math.floor(num / 10)
1865238652
28652865
386586
4868
580 (loop ends)

Five digits → five lines. When num becomes 0, the while condition fails.

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

Print, then drop the last digit until the value is zero.

JavaScript
let num = 86523;

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

How It Works

1. Guard with a while. Keep looping while num is not zero.

2. Print before shrinking. console.log(num) shows the current value on its own line.

3. Drop one digit. Math.floor(num / 10) removes the rightmost digit — five digits means five lines.

Example 2 — User Input Number

Read the start value with prompt, validate, and use Math.abs for negatives.

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

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

How It Works

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

2. Normalize sign. Math.abs lets negative input shrink the same way as positive.

3. Same shrink core. Only the source of num changes — print then divide matches Example 1.

Example 3 — Compact num = 123

Same while loop with a smaller start for a quick visual check.

JavaScript
let num = 123;

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

How It Works

1. Shrink the start. Only the initial num changes — still print then divide.

2. Easier to trace. Three lines — useful while learning Math.floor(num / 10).

3. Same stop rule. The loop ends when the last digit becomes 0 after division.

Edge Cases & Pitfalls

Check these before calling the solution done.

Update

Always assign num = Math.floor(num / 10)

Forgetting the update leaves num unchanged — infinite loop.

Integer divide

Use Math.floor, not plain /

Plain division keeps decimals (e.g. 865.2) and never reaches exact 0 the same way.

num = 0

Empty output

The loop body never runs when the start value is already zero.

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 removes one digit — for d digits the loop runs d times.

Key Takeaways

  • Rule: print num, then num = Math.floor(num / 10).
  • Stop: while (num !== 0) ends when all digits are gone.
  • One line each: every console.log is a full shrinking step.
  • Complexity: O(d) — one step per digit.

One line: while the number is nonzero, log it and integer-divide by 10.

Frequently Asked Questions

Math.floor(num / 10) discards the remainder: Math.floor(86523 / 10) becomes 8652, then 865, then 86, then 8.
The step count equals the digit count — unknown until you see the number. while (num !== 0) stops when all digits are gone.
Program 59 uses nested loops on a grid. Program 60 uses one while loop and integer division on a single number.
This pattern logs a full value each step — console.log(num) ends each line. There is no same-row append.
A trailing 0 is removed on the next step — 120 becomes 12, then 1.
Yes — use Math.abs(num) before the loop so the while condition still works (see Example 2).
O(d) where d is the number of digits — each iteration removes exactly one digit.
while (num !== 0) skips the body — nothing is printed.

Did you know?

Each iteration prints the current number, then num = Math.floor(num / 10) drops the last digit — runtime is O(d) for d digits.

Next: Growing Reverse Number Pattern

Continue with the next pattern in the JavaScript number-pattern series.

Program 61 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