JavaScript Mirror Number Pattern (0-Centered)

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

What Is This Pattern?

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.

How to Solve It

Count i down from max + 1; append i..max, then 0, then max..i.

MethodIdeaBest for
Three partsAscend, 0, descendLearning, interviews
prompt maxSame loops; clamp max to 1–9Interactive practice
Spaced digitsline += j + " " in both halvesReadable 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

GoalPattern
Set maxconst maxN = 9;
Outer loopfor (let i = maxN + 1; i >= 1; i--)
Left halffor (let j = i; j <= maxN; j++) line += j;
Centerline += "0";
Right halffor (let k = maxN; k >= i; k--) line += k;
End rowconsole.log(line);
Spacedline += j + " "; / line += "0 ";

Printing Numbers vs Starting a New Line

APIEffectUse for
line += j / line += "0"Stays on the same rowEach digit
console.log(line)Ends the lineAfter 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.

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 result max = 5 · rows = 6
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — maxN = 3

Trace each outer i, the left half, the center 0, and the right mirror.

iLeft i..3CenterRight 3..iPrinted row
4none0none0
3303303
22303223032
112303211230321

Outer i starts at max + 1 so the first row is only the center zero.

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

How It Works

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

How It Works

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

How It Works

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

About n + 1 rows, each printing up to 2n + 1 digits → O(n²).

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.

Next: Mirrored Number Pattern

Continue with the next 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