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.
Approach
How to Solve It
Loop while num !== 0: log the current value, then integer-divide by 10.
Method
Idea
Best for
While + divide
Print, then Math.floor(num / 10)
Classic digit-removal demos
Compact
Same loop with a smaller start (e.g. 123)
Quick dry-runs
Pseudocode
Pseudocode
while num !== 0:
print num
num = floor(num / 10)
Cheat sheet
Goal
Pattern
Loop
while (num !== 0)
Print current
console.log(num);
Drop last digit
num = Math.floor(num / 10);
Negatives
num = Math.abs(parsed); before the loop
Lines printed
Digit count of the start value
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(num)
Ends the current row
Every shrinking value
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 shrinking sequence updates instantly.
Positive integers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.
Live resultstart = 86523 · 5 lines
86523
8652
865
86
8
Trace
Worked Walkthrough
Trace num = 86523 — print first, then divide.
Step
Printed
After Math.floor(num / 10)
1
86523
8652
2
8652
865
3
865
86
4
86
8
5
8
0 (loop ends)
Five digits → five lines. When num becomes 0, the while condition fails.
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
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);
}
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);
}
}