JavaScript Alphabet Pattern (Symmetric with Star Center)
Beginner
8 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A symmetric alphabet pattern with a star center keeps a fixed row width while letter halves shrink and an even number of * characters grow in the middle.
Each row has three parts: ascending letters from A to the row end, then 2*(top - end) stars, then the mirror descending back to A. When stars = 0, the peak letter appears twice (ABCDEEDCBA).
Approach
How to Solve It
Outer loop counts the end letter down from top to A. Each row builds left, stars, then right.
Method
Idea
Best for
Three inner loops
Left letters, star loop, right mirror
Learning, interviews, exams
"*".repeat(stars)
Same shape; fill the center in one call
Shorter demos once loops click
Pseudocode
Pseudocode
base = code of 'A'
top = base + rows - 1
for i from top down to base:
line = ""
for j from base to i: // left A..i
append fromCharCode(j)
stars = 2 * (top - i)
for s from 1 to stars: // center *
append '*'
for m from i down to base: // right i..A
append fromCharCode(m)
print line
Cheat sheet
Goal
Pattern
Top for n rows
const top = "A".charCodeAt(0) + rows - 1;
Walk end letter down
for (let i = top; i >= base; i--)
Left half
for (let j = base; j <= i; j++) line += String.fromCharCode(j);
Star count
const stars = 2 * (top - i);
Right half
for (let m = i; m >= base; m--) line += String.fromCharCode(m);
Star shortcut
line += "*".repeat(stars);
Row width
Always 2 * rows characters
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch
Stays on the same row
Each letter and each *
console.log(line)
Ends the current row
After left + stars + right
Append characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the symmetric star-center pattern updates instantly — including top letter and fixed width.
Whole numbers from 1 to 8. Tap a chip or type a value — the preview redraws as you go.
Trace each end letter i, the star count, and the finished row (width always 8).
i
Left
Stars
Right
Printed row
D
ABCD
0
DCBA
ABCDDCBA
C
ABC
2
CBA
ABC**CBA
B
AB
4
BA
AB****BA
A
A
6
A
A******A
Each row has 2n = 8 characters. Over n rows that is 2n² characters total — still O(n²) time.
Code
JavaScript Programs
Three complete programs: fixed top letter, prompt row count, and a "*".repeat shortcut. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed top = 'E'
Hard-coded height with three explicit inner loops — ideal for first demos and screenshots.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = top; i >= base; i--) {
let line = "";
for (let j = base; j <= i; j++) {
line += String.fromCharCode(j);
}
const stars = 2 * (top - i);
for (let s = 0; s < stars; s++) {
line += "*";
}
for (let m = i; m >= base; m--) {
line += String.fromCharCode(m);
}
console.log(line);
}
2. Outer loop picks the letter half end.i counts from top down to base.
3. Build the three parts. Left: A…i. Stars: 2*(top - i). Right: i…A.
4. Break the line.console.log(line) after all three parts finish.
When i is E, stars = 0 and you get ABCDEEDCBA. As i falls, letters shrink and stars grow; width stays 10.
Example 2 — User Input Version
Read the row count at runtime with prompt. Validate with parseInt and clamp to 26 for A–Z demos.
JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
const base = "A".charCodeAt(0);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number of rows >= 1.");
} else {
if (rows > 26) rows = 26;
const top = base + rows - 1;
for (let i = top; i >= base; i--) {
let line = "";
for (let j = base; j <= i; j++) {
line += String.fromCharCode(j);
}
const stars = 2 * (top - i);
for (let s = 0; s < stars; s++) {
line += "*";
}
for (let m = i; m >= base; m--) {
line += String.fromCharCode(m);
}
console.log(line);
}
}
1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).
2. Validate and set top. Reject NaN or non-positive values; cap at 26; top = base + rows - 1.
3. Same three-part builder. Only the source of top changes — the print logic matches Example 1.
Example 3 — "*".repeat(stars)
Fill the center gap in one call — same shape, no explicit star loop.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = top; i >= base; i--) {
let line = "";
for (let j = base; j <= i; j++) {
line += String.fromCharCode(j);
}
const stars = 2 * (top - i);
line += "*".repeat(stars);
for (let m = i; m >= base; m--) {
line += String.fromCharCode(m);
}
console.log(line);
}
Break the row: call console.log(line) only after all three parts.
Complexity:O(n²) time from n rows × 2n characters.
One line: for each end letter from top to A, print A…i, then 2*(top-i) stars, then i…A.
Frequently Asked Questions
The left half appends A…E, and the right half appends E…A. When there are zero stars on the first row, E appears as the last character of the left half and the first character of the right half.
Stars per row are 2*(top − end) using letter codes. For top=E: 0, 2, 4, 6, 8 stars as end goes E, D, C, B, A.
Because it is computed as 2*(top − i), which is always a multiple of 2.
Yes. You can start the right half from i − 1 instead of i when there are zero stars, so the first row becomes ABCDEDCBA.
line += builds the full row string. console.log() inside an inner loop would log one character per line. Log once after left, stars, and right finish.
O(n²) for n rows. Each row prints a total of 2n characters (letters + stars), repeated across n rows.
Yes. line += '*'.repeat(stars) fills the whole gap in one call. Nested star loops are fine for learning; '*'.repeat(stars) is a handy shortcut later.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so top stays within A–Z.
🤔
Did you know?
Each row is: ascending letters from A to the row end, then 2*(top - end) stars, then descending letters back to A. The middle letter appears twice when star count is 0, producing ABCDEEDCBA on the first row. Total width stays constant at 2n characters per row.