A 0-centered descending mirror prints ascending digits, a fixed 0, then descending digits — rows grow as the outer index drops toward 1.
Remember
Rule: for i = max+1 down to 1
print i..max, then 0, then max..i
0
909
89098
…
1234567890987654321 ← max = 9
Unlike Program 27’s growing peak, the center is always 0 — a natural step after the palindrome triangle in Program 27.
Approach
How to Solve It
Count i down from max + 1; append i..max, then 0, then max..i.
Method
Idea
Best for
Three parts
Ascend, 0, descend
Learning, interviews
prompt max
Same loops; clamp max to 1–9
Interactive practice
Spaced digits
line += j + " " in both halves
Readable output
Pseudocode
Pseudocode
for i from max + 1 down to 1:
line = ""
for j from i to max:
line += j
line += "0"
for k from max down to i:
line += k
print line (with newline)
Cheat sheet
Goal
Pattern
Set max
const maxN = 9;
Outer loop
for (let i = maxN + 1; i >= 1; i--)
Left half
for (let j = i; j <= maxN; j++) line += j;
Center
line += "0";
Right half
for (let k = maxN; k >= i; k--) line += k;
End row
console.log(line);
Spaced
line += j + " "; / line += "0 ";
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j / line += "0"
Stays on the same row
Each digit
console.log(line)
Ends the line
After all three parts
Build the full row with +=, then break once with console.log. Putting console.log inside an inner loop prints one digit per line.
Try it
Live Preview
Change the max digit and the 0-centered mirror updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultmax = 5 · rows = 6
0
505
45054
3450543
234505432
12345054321
Trace
Worked Walkthrough — maxN = 3
Trace each outer i, the left half, the center 0, and the right mirror.
i
Left i..3
Center
Right 3..i
Printed row
4
none
0
none
0
3
3
0
3
303
2
23
0
32
23032
1
123
0
321
1230321
Outer i starts at max + 1 so the first row is only the center zero.
Code
JavaScript Programs
Three complete programs: fixed max = 9, prompt max digit, and spaced digits. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed max = 9
Hard-coded max digit — outer i = 10..1, left i..9, center 0, right 9..i.
JavaScript
for (let i = 10; i >= 1; i--) {
let line = "";
for (let j = i; j < 10; j++) {
line += j;
}
line += "0";
for (let k = 9; k >= i; k--) {
line += k;
}
console.log(line);
}
1. Outer countdown.i runs from 10 down to 1 — start of the left half (or empty when i = 10).
2. Left then center. Append j from i to 9, then append "0".
3. Right mirror. Append k from 9 down to i; then console.log(line).
Example 2 — prompt Max Digit
Read the max digit at runtime; clamp to 1–9 and start the outer loop at maxN + 1.
JavaScript
let maxN = parseInt(prompt("Enter max digit (1-9):"), 10);
if (!Number.isFinite(maxN) || maxN < 1) {
console.log("Please enter a digit from 1 to 9.");
} else {
if (maxN > 9) maxN = 9;
for (let i = maxN + 1; i >= 1; i--) {
let line = "";
for (let j = i; j <= maxN; j++) {
line += j;
}
line += "0";
for (let k = maxN; k >= i; k--) {
line += k;
}
console.log(line);
}
}
1. Prompt and clamp. Parse the answer; reject bad input; cap at 9.
2. Generalize bounds. Outer starts at maxN + 1; both halves use maxN instead of 9.
3. Same structure. Ascend, 0, descend — identical shape at any max.
Example 3 — Spaced Digits
Keep maxN = 5 but append a space after each digit (and after 0).
JavaScript
const maxN = 5;
for (let i = maxN + 1; i >= 1; i--) {
let line = "";
for (let j = i; j <= maxN; j++) {
line += j + " ";
}
line += "0 ";
for (let k = maxN; k >= i; k--) {
line += k + " ";
}
console.log(line);
}
1. Same structure. Outer countdown and both half-loops match Example 2.
2. Only append changes. Use line += j + " ", line += "0 ", and line += k + " ".
3. Same shape. Values and row count stay the same — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
no 0
Missing center
Forgetting line += "0" glues left and right halves together without the zero pivot.
i = max
Wrong outer start
Starting at i = maxN skips the lone 0 row. Use i = maxN + 1.
log inside
Vertical digits
If console.log is inside either half-loop, each digit lands on its own line. Log only after all three parts.
maxN = 1
Smallest mirror
Output is 0 then 101.
max > 9
Multi-digit cells
Values above 9 print as two characters and break the single-digit look. Clamp to 9 in input examples.
NaN input
Validate parseInt
Letters or empty prompt yield NaN — check Number.isFinite(maxN) && maxN >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(n) for the current line string
About n + 1 rows, each printing up to 2n + 1 digits → O(n²).
Remember
Key Takeaways
Rule: print i..max, then 0, then max..i.
Start at max+1: first row is only the center zero.
Write vs log:line += … builds; console.log(line) breaks.
Complexity:O(n²) for max digit n.
One line: grow both sides toward the max digit while keeping a fixed 0 in the middle of every row.
Frequently Asked Questions
line += "0" sits between the ascending and descending loops, creating a fixed center on every row.
When i = max + 1, both side loops are empty — only 0 is printed.
Starting one past the max digit makes the first row have empty left and right halves, giving the single 0 row.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Replace 9 with maxN and start i at maxN + 1 — see Example 2.
Use line += j + " " and line += k + " " (and "0 ") — see Example 3.
O(n²) for max digit n because each row prints O(n) digits and there are O(n) rows.
Use parseInt with Number.isFinite and clamp maxN to 1..9 so loop bounds stay valid.
Two rows: 0 and 101 — the smallest non-trivial mirror with a zero center.
🤔
Did you know?
This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.