Shape Rule
0 at center
Each row is ascending digits, 0, then descending digits — a mirror around zero.

The 0-centered descending mirror pattern prints 0, 909, 89098, … up to 1234567890987654321 — a natural step after the palindrome triangle in Program 27. This tutorial covers three nested loops, a fixed zero center, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
0 at center
Each row is ascending digits, 0, then descending digits — a mirror around zero.
i = 10..1
for (let i = 10; i >= 1; i--) — descending outer loop grows each row.
i..9
for (let j = i; j < 10; j++) prints the ascending left half.
9..i
for (let k = 9; k >= i; k--) mirrors digits after the zero.
max 3–9
Pick a max digit and draw the 0-centered mirror pattern in the browser.
Complexity
Total prints grow as n² for max digit n.
A 0-centered descending mirror number pattern prints ascending digits, a fixed 0, then descending digits on each row. With max digit 9, the output grows from 0 to 1234567890987654321.
In JavaScript you use a descending outer loop i = 10..1, ascending inner loop j = i..9, print 0, then descending inner loop k = 9..i.
It introduces three coordinated loops with a fixed center — a step up from Program 27’s two-loop palindrome.
line += "0" between both inner loops.
Ascending digits grow as i decreases.
Descending mirror completes each row.
Follow Program 27; continue to Program 29 (spaced mirror) next.
In short: for each i from 10 down to 1, append i..9, then 0, then 9..i, then console.log(line).
Given max digit 9, print 10 rows of a 0-centered mirror: for each descending i, append i..9, then 0, then 9..i on the same line.
// max_n = 9 (conceptual shape)
// 0
// 909
// 89098
// …
// 1234567890987654321 | Item | Type | Description |
|---|---|---|
max_n | int | Highest digit on each side — typically 9; outer loop starts at max_n + 1. |
i | int | Descending outer loop — controls how many digits appear on each side. |
j | int | Ascending loop — appends i..max (left half). |
k | int | Descending loop — appends max_n..i (right half). |
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
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Three loops + 0 | 0, 909, 89098, … | Learning and interviews |
| Custom max digit | parseInt(prompt(...), 10) | Flexible console programs |
| Spaced output | line += j + " " | Easier reading for wide rows |
| Goal | Pattern |
|---|---|
| Walk rows | for (let i = 10; i >= 1; i--) |
| Left half | for (let j = i; j < 10; j++) { line += j; } |
| Center zero | line += "0" |
| Right half | for (let k = 9; k >= i; k--) { line += k; } |
| End the row | console.log(line) |
| Custom max | for (let i = max_n + 1; i >= 1; i--) with j <= max_n and k >= i |
Same 0-centered mirror — different ways to control max digit and formatting.
i = max_n+1..1Descending — grows each row
j = i..maxAscending digits
line += "0"Fixed zero between loops
i = max_n+1First row prints only 0
Reach for this pattern when teaching three coordinated loops with a fixed center character.
Natural follow-up after Program 27 — introduces a fixed 0 center and descending outer loop.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 27 (palindrome triangle) and Program 29 (spaced mirror) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a max digit between 3 and 9 and draw the 0-centered mirror pattern in the browser.
Three complete JavaScript programs — fixed max digit, custom max input, and spaced output variant. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print ten rows of the 0-centered mirror with max digit 9 and outer loop i = 10..1.
max_n = 9Hard-coded max digit — ideal for first demos and screenshots.
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);
} When i = 10, both inner loops are empty — output is just 0. When i = 9, append 9, then 0, then 9 — output 909. When i = 1, the full mirror 1234567890987654321 appears.
Read the max digit with prompt() and generalize loop bounds.
Read max_n with prompt() and parseInt(); outer loop runs from max_n + 1 down to 1.
const maxInput = prompt("Enter max digit (1-9):");
let max_n = parseInt(maxInput, 10);
if (!Number.isFinite(max_n) || max_n < 1) max_n = 1;
if (max_n > 9) max_n = 9;
for (let i = max_n + 1; i >= 1; i--) {
let line = "";
for (let j = i; j <= max_n; j++) {
line += j;
}
line += "0";
for (let k = max_n; k >= i; k--) {
line += k;
}
console.log(line);
} Replace hard-coded 9 and 10 with max_n and max_n + 1. Clamp input to 1..9 so loop bounds stay valid. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.
Add a space between digits for easier reading on wide rows.
Keep max_n = 9 but print each digit followed by a space in both loops.
const max_n = 9;
for (let i = max_n + 1; i >= 1; i--) {
let line = "";
for (let j = i; j <= max_n; j++) {
line += j + " ";
}
line += "0 ";
for (let k = max_n; k >= i; k--) {
line += k + " ";
}
console.log(line);
} Only the append changes — line += j + " " and line += k + " ". The three-loop structure and 0 center stay the same as Example 1.
console.log is built in; use prompt() when reading input. Set loop variables i, j, k with max digit 9.
for (let i = 10; i >= 1; i--) — descending outer loop; one row per iteration.
for (let j = i; j < 10; j++) — appends digits i..9 (left half).
line += "0" — fixed center between both inner loops.
for (let k = 9; k >= i; k--) — appends digits 9..i, then console.log(line).
Rows grow toward 1234567890987654321 — O(n²) time, O(1) extra memory.
max_n = 9 (selected rows)Trace selected outer-loop values of i, the left half, center, right half, and full row output.
i | Left (j) | Center | Right (k) | Row output |
|---|---|---|---|---|
10 | (none) | 0 | (none) | 0 |
9 | 9 | 0 | 9 | 909 |
8 | 8, 9 | 0 | 9, 8 | 89098 |
2 | 2..9 | 0 | 9..2 | 23456789098765432 |
1 | 1..9 | 0 | 9..1 | 1234567890987654321 |
When i = max_n + 1, both inner loops are empty — only 0 prints. Each row grows as i decreases.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j < 10 to j <= 10 and watch the left half grow differently.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 29 for a spaced mirror with alignment gaps.
Practice line += vs console.log(line) without complex math.
Example: put console.log(line) inside an inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use line += j + " " in both inner loops.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for max_n = 5 — grows toward a full mirror row of 11 digits.
Pair the pattern with Number.isFinite and clamp checks.
Example: reject max_n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, j, and k on paper for max_n = 3 before coding — when i = 4 only 0 prints.
Small habits that keep number-pattern code clean.
Use j < max_n + 1 or j <= max on the left, and k >= i on the right — match hard-coded 9 and 10 when generalizing.
prompt()Validate parseInt(prompt(), 10) with Number.isFinite so bad input does not produce NaN.
console.log(line) OutsideOnly call console.log(line) after all three parts finish the row.
Mark the ascending half and mirror half for each row before coding.
Trace i = 4..1 on paper before coding the full max_n = 9 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside an inner loop.
Mistakes that commonly break 0-centered mirror patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += j, line += "0", or line += k; console.log(line) only after all three parts.
Starting at i = max skips the single-0 first row.
→ Start the outer loop at max_n + 1 so the first row prints only 0.
Without line += "0", rows concatenate digits with no fixed center.
→ Print 0 between the ascending and descending inner loops.
Using j <= max on the left but k > i on the right breaks symmetry.
→ Mirror bounds: left j = i..max, right k = max..i.
Letters or empty input yield NaN from parseInt(prompt(), 10).
→ Validate with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
Both inner loops empty — output is just 0.
Clamp or reject — loops need a positive max digit.
Single-digit pattern — clamp to 9 for console demos.
Two rows: 0 and 101.
parseInt(prompt(), 10) yields NaN — validate with Number.isFinite first.
Output grows as max² digits — fine for labs, noisy beyond 9.
Try these variations to lock in the pattern.
0 with * or #max_n = 4 and trace every row0 between the ascending loop (j) and descending loop (k) on every row.line += builds the row; console.log(line) ends it — keep them in the right order.max_n in 1..9 for interactive programs; max_n = 1 gives rows 0 and 101.line += j + " " in both loops for easier reading on wide rows.Quick Takeaway: outer loop i = max_n+1..1, ascending j = i..max, line += "0", descending k = max..i, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
The 0-centered descending mirror pattern is a compact lesson in three coordinated loops: print ascending i..max_n, a fixed 0, then descending max_n..i. Master the fixed-max_n = 9 version, then try custom max and spaced output.
Practice the three examples above, then continue to Program 29 for the spaced mirror number pattern.
Start the outer loop at max_n + 1 for the single-0 first row — validate max_n when reading from the console.
for (let i = max_n + 1; i >= 1; i--) in the outer loopfor (let j = i; j <= max_n; j++)line += "0" between inner loopsfor (let k = max_n; k >= i; k--)parseInt(prompt(), 10) with Number.isFinite and clamp max_n to 1..9console.log(line) inside either inner loopmax_n instead of max_n + 10 between loopsj and ki = max_n + 1 edge casePrint the pattern the beginner-friendly way.
j..max, 0, max..i
DefinitionLeft half
CodeCenter
CodeRight half
ShapeO(n²) time
AnalysisThis pattern prints ascending digits from i to 9, a fixed 0 in the center, then descending digits from 9 down to i. As i decreases, each row grows into the long mirror 1234567890987654321.
Move on to the spaced mirror number pattern in the JavaScript number-pattern series.
12 people found this page helpful