JavaScript Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

A number & asterisk mirror prints ascending digits 1..i, a growing ** center, then descending digits i..1 — three inner loops on each row.

Remember
Rule: for i = n down to 1
      print 1..i, then (n−i)×"**", then i..1

1234554321
1234**4321
123****321
12******21
1********1     ← n = 5

As i shrinks, the star center grows — a natural step after odd-length rows in Program 22.

How to Solve It

Count i down from n; print left digits, then ** pairs, then right digits.

MethodIdeaBest for
Three inner loopsAscend, stars, descendLearning, interviews
prompt inputSame loops; read size at runtimeInteractive practice
Custom fillReplace "**" with "##" etc.Variant practice

Pseudocode

Pseudocode
for i from n down to 1:
    line = ""
    for j from 1 to i:
        line += j
    for k from i to n - 1:
        line += "**"
    for m from i down to 1:
        line += m
    print line (with newline)

Cheat sheet

GoalPattern
Set sizeconst n = 5;
Outer loopfor (let i = n; i >= 1; i--)
Left digitsfor (let j = 1; j <= i; j++) line += j;
Center starsfor (let k = i; k < n; k++) line += "**";
Right digitsfor (let m = i; m >= 1; m--) line += m;
End rowconsole.log(line);
Custom fillline += "##"; in the center loop

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowDigits and **
console.log(line)Ends the lineAfter all three inner loops

Build the full row with +=, then break once with console.log. Putting console.log inside an inner loop splits the mirror across many lines.

Live Preview

Change size n and the mirror pattern updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · rows = 5
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 3

Trace each outer value of i, the left digits, star pairs, and right mirror.

iLeft 1..iStarsRight i..1Printed row
3123none321123321
2121× **2112**21
112× **11****1

Star pairs = n − i. Digits shrink as the center grows — every row stays width 2n characters of content (digits + star chars).

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and a custom center fill. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 5

Hard-coded size — three inner loops build left digits, **, then right digits.

JavaScript
const n = 5;

for (let i = n; i >= 1; i--) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += j;
  }
  for (let k = i; k < n; k++) {
    line += "**";
  }
  for (let m = i; m >= 1; m--) {
    line += m;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer countdown. i runs from n down to 1 — digit half-width for that row.

2. Left then center. Append 1..i, then append "**" exactly n − i times.

3. Right mirror. Append i..1, then console.log(line).

Example 2 — prompt Input

Read size at runtime; the three-loop structure stays the same.

JavaScript
const n = parseInt(prompt("Enter the size:"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = n; i >= 1; i--) {
    let line = "";
    for (let j = 1; j <= i; j++) {
      line += j;
    }
    for (let k = i; k < n; k++) {
      line += "**";
    }
    for (let m = i; m >= 1; m--) {
      line += m;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate first. Reject NaN or non-positive values before looping.

3. Same three loops. Center star count still equals n − i for the chosen size.

Example 3 — Custom Fill ##

Keep n = 5 but replace asterisk pairs with hash pairs in the center.

JavaScript
const n = 5;
const fill = "##";

for (let i = n; i >= 1; i--) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += j;
  }
  for (let k = i; k < n; k++) {
    line += fill;
  }
  for (let m = i; m >= 1; m--) {
    line += m;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same structure. Outer and digit loops match Example 1.

2. Swap the fill. The center loop appends fill instead of "**".

3. Keep two characters. A two-character fill keeps the same visual width as **.

Edge Cases & Pitfalls

Check these before calling the solution done.

one *

Single asterisk

Appending "*" instead of "**" grows the center by 1 and breaks the even-width mirror.

log inside

Broken rows

If console.log is inside any inner loop, the mirror splits across lines. Log only after all three loops.

k <= n

Wrong star count

Use k < n (runs n − i times). Using k <= n adds one extra pair.

n = 1

Single mirror

Output is just 11 — left 1, no stars, right 1.

n ≤ 0

Empty output

The outer loop never runs. Guard prompt input with n >= 1.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

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

Each of n rows builds about 2n characters of content → O(n²) total work.

Key Takeaways

  • Rule: print 1..i, then (n − i) pairs of **, then i..1.
  • Three loops: left digits, center fill, right mirror.
  • Write vs log: line += … builds; console.log(line) breaks.
  • Complexity: O(n²) for size n.

One line: shrink the digit halves each row, and grow a two-character center fill to keep the mirror balanced.

Frequently Asked Questions

Each iteration appends two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern appends ascending numbers 1..i, then stars, then descending numbers i..1 on the same line.
Because i starts at n and decreases — each row prints fewer digits and more stars in the center.
line += j stays on the same row while building characters. console.log(line) prints the completed row and adds a newline.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with "##", two spaces, or any two-character fill (see Example 3).
O(n²) for size n because each row prints O(n) characters overall.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row appends 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.

Next: Centered Continuous Pyramid

Continue with the next pattern in the JavaScript number-pattern series.

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