0-Centered Descending Mirror Number Pattern in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: Sep 2026
🎯 3 Code Examples
🚀 Live Preview
Three Loops

What You’ll Learn

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.

Shape Rule

0 at center

Each row is ascending digits, 0, then descending digits — a mirror around zero.

Outer Loop

i = 10..1

for (let i = 10; i >= 1; i--) — descending outer loop grows each row.

Left Loop (j)

i..9

for (let j = i; j < 10; j++) prints the ascending left half.

Right Loop (k)

9..i

for (let k = 9; k >= i; k--) mirrors digits after the zero.

Live Preview

max 3–9

Pick a max digit and draw the 0-centered mirror pattern in the browser.

O(n²)

Complexity

Total prints grow as for max digit n.

Introduction

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.

Why it matters?

It introduces three coordinated loops with a fixed center — a step up from Program 27’s two-loop palindrome.

Key Highlights

Fixed 0 center

line += "0" between both inner loops.

Left j = i..9

Ascending digits grow as i decreases.

Right k = 9..i

Descending mirror completes each row.

Series Foundation

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

📝 Problem & Approach

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.

JavaScript
// max_n = 9 (conceptual shape)
// 0
// 909
// 89098
// …
// 1234567890987654321

Inputs & Outputs

ItemTypeDescription
max_nintHighest digit on each side — typically 9; outer loop starts at max_n + 1.
iintDescending outer loop — controls how many digits appear on each side.
jintAscending loop — appends i..max (left half).
kintDescending loop — appends max_n..i (right half).

Minimal workflow

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
    console.log(line)

Approach comparison

ApproachIdeaBest for
Three loops + 00, 909, 89098, …Learning and interviews
Custom max digitparseInt(prompt(...), 10)Flexible console programs
Spaced outputline += j + " "Easier reading for wide rows

⚡ Quick Reference

GoalPattern
Walk rowsfor (let i = 10; i >= 1; i--)
Left halffor (let j = i; j < 10; j++) { line += j; }
Center zeroline += "0"
Right halffor (let k = 9; k >= i; k--) { line += k; }
End the rowconsole.log(line)
Custom maxfor (let i = max_n + 1; i >= 1; i--) with j <= max_n and k >= i

📋 Fixed Max vs Custom Max vs Spaced Output

Same 0-centered mirror — different ways to control max digit and formatting.

Outer loop
i = max_n+1..1

Descending — grows each row

Left half
j = i..max

Ascending digits

Center
line += "0"

Fixed zero between loops

Learning tip
i = max_n+1

First row prints only 0

Context

When This Pattern Shows Up

Reach for this pattern when teaching three coordinated loops with a fixed center character.

  1. Post palindrome exercise

    Natural follow-up after Program 27 — introduces a fixed 0 center and descending outer loop.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Compare Program 27 (palindrome triangle) and Program 29 (spaced mirror) next.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Choose a max digit between 3 and 9 and draw the 0-centered mirror pattern in the browser.

Try 5, 7, or 9. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

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.

📚 Getting Started

Print ten rows of the 0-centered mirror with max digit 9 and outer loop i = 10..1.

Example 1 — Fixed max_n = 9

Hard-coded max digit — ideal for first demos and screenshots.

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

How It Works

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.

📈 Custom Max Digit

Read the max digit with prompt() and generalize loop bounds.

Example 2 — User Input Max Digit

Read max_n with prompt() and parseInt(); outer loop runs from max_n + 1 down to 1.

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

How It Works

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.

⚡ Spaced Output

Add a space between digits for easier reading on wide rows.

Example 3 — Spaced Digits

Keep max_n = 9 but print each digit followed by a space in both loops.

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

How It Works

Only the append changes — line += j + " " and line += k + " ". The three-loop structure and 0 center stay the same as Example 1.

🧠 How the Algorithm Prints Rows

1

Set up

console.log is built in; use prompt() when reading input. Set loop variables i, j, k with max digit 9.

Setup
2

Outer loop walks rows

for (let i = 10; i >= 1; i--) — descending outer loop; one row per iteration.

Row
3

Ascending inner loop (j)

for (let j = i; j < 10; j++) — appends digits i..9 (left half).

Ascend
4

Print center zero

line += "0" — fixed center between both inner loops.

Center
5

Descending inner loop (k)

for (let k = 9; k >= i; k--) — appends digits 9..i, then console.log(line).

Mirror
=

0-centered mirror complete

Rows grow toward 1234567890987654321O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — max_n = 9 (selected rows)

Trace selected outer-loop values of i, the left half, center, right half, and full row output.

iLeft (j)CenterRight (k)Row output
10(none)0(none)0
9909909
88, 909, 889098
22..909..223456789098765432
11..909..11234567890987654321

When i = max_n + 1, both inner loops are empty — only 0 prints. Each row grows as i decreases.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j < 10 to j <= 10 and watch the left half grow differently.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: continue to Program 29 for a spaced mirror with alignment gaps.

3. Console Formatting Drills

Practice line += vs console.log(line) without complex math.

Example: put console.log(line) inside an inner loop by mistake.

4. Spaced formatting

Add spaces between digits once the two-loop structure works.

Example: use line += j + " " in both inner loops.

5. Complexity Intuition

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.

6. Input Validation Labs

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.

Advantages

Why this pattern earns a permanent spot in beginner JavaScript courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    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.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Keep Bounds Consistent

    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.

  2. 2. Validate prompt()

    Validate parseInt(prompt(), 10) with Number.isFinite so bad input does not produce NaN.

  3. 3. Keep console.log(line) Outside

    Only call console.log(line) after all three parts finish the row.

  4. 4. Trace i, j, and k on Paper

    Mark the ascending half and mirror half for each row before coding.

  5. 5. Dry-Run max_n = 3

    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.

Common Pitfalls

Mistakes that commonly break 0-centered mirror patterns.

  1. 1. Newline Inside the Inner Loop

    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.

  2. 2. Wrong Outer Loop Start

    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.

  3. 3. Forgetting the Center Zero

    Without line += "0", rows concatenate digits with no fixed center.

    → Print 0 between the ascending and descending inner loops.

  4. 4. Mismatched j and k Bounds

    Using j <= max on the left but k > i on the right breaks symmetry.

    → Mirror bounds: left j = i..max, right k = max..i.

  5. 5. Unchecked input

    Letters or empty input yield NaN from parseInt(prompt(), 10).

    → Validate with Number.isFinite and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

i = max_n+1

First row only 0

Both inner loops empty — output is just 0.

max_n = 0

Invalid max

Clamp or reject — loops need a positive max digit.

max > 9

Out of range

Single-digit pattern — clamp to 9 for console demos.

max_n = 1

Smallest mirror

Two rows: 0 and 101.

Bad input

Non-numeric input

parseInt(prompt(), 10) yields NaN — validate with Number.isFinite first.

Large max

Large max digit

Output grows as max² digits — fine for labs, noisy beyond 9.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Palindrome triangle

  • Two inner loops without a zero center
  • Review Program 27

2. Spaced mirror

  • Mirror with alignment spaces between halves
  • Continue with Program 29

3. Custom center char

  • Replace 0 with * or #
  • Same three-loop structure

4. Smaller max demo

  • Run with max_n = 4 and trace every row
  • Compare with Example 2 output

Notes

  • Center rule. Print 0 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.
  • Validate max_n in 1..9 for interactive programs; max_n = 1 gives rows 0 and 101.
  • Add spaces with 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).

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Spaced output (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Use for (let i = max_n + 1; i >= 1; i--) in the outer loop
  • Ascend with for (let j = i; j <= max_n; j++)
  • Print line += "0" between inner loops
  • Mirror with for (let k = max_n; k >= i; k--)
  • Validate parseInt(prompt(), 10) with Number.isFinite and clamp max_n to 1..9

❌ Don’t

  • Call console.log(line) inside either inner loop
  • Start outer loop at max_n instead of max_n + 1
  • Forget the center 0 between loops
  • Use mismatched bounds on j and k
  • Ignore bad console input in user-facing demos
  • Skip the i = max_n + 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this 0-centered pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

j = i..max

Left half

Code
+ 03

line += "0"

Center

Code
04

k = max..i

Right half

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

line += "0" sits between the ascending and descending loops, creating a fixed center on every row.
When i = 10, the j loop never runs and the k loop never runs — only 0 is printed.
With max digit 9, i runs from max_n+1 down to 1. When i = 10 both side loops are empty, giving the single 0 row.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward 9 on both sides as i decreases.
Replace 9 with max_n and start i at max_n+1 — see Example 2.
Use line += j + " " and line += k + " " in the loops instead of line += j.
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 max_n to 1..9 — see Example 2 notes.
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 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.

Continue to Program 29

Move on to the spaced mirror number pattern in the JavaScript number-pattern series.

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