JavaScript Number Mirror Pattern (Asterisk Center)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Count i down from n; print left digits, then ** pairs, then right digits.
Method
Idea
Best for
Three inner loops
Ascend, stars, descend
Learning, interviews
prompt input
Same loops; read size at runtime
Interactive practice
Custom fill
Replace "**" 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
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = n; i >= 1; i--)
Left digits
for (let j = 1; j <= i; j++) line += j;
Center stars
for (let k = i; k < n; k++) line += "**";
Right digits
for (let m = i; m >= 1; m--) line += m;
End row
console.log(line);
Custom fill
line += "##"; in the center loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Digits and **
console.log(line)
Ends the line
After 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.
Try it
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.
Trace each outer value of i, the left digits, star pairs, and right mirror.
i
Left 1..i
Stars
Right i..1
Printed row
3
123
none
321
123321
2
12
1× **
21
12**21
1
1
2× **
1
1****1
Star pairs = n − i. Digits shrink as the center grows — every row stays width 2n characters of content (digits + star chars).
Code
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);
}
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);
}
}
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);
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(n) for the current line string
Each of n rows builds about 2n characters of content → O(n²) total work.
Remember
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.