JavaScript Alphabet Pattern (Symmetric with Star Center)

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

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.

Remember
Rule: A..end | even stars | end..A  (width = 2n)

ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********A     ← 5 rows (top = E)

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

How to Solve It

Outer loop counts the end letter down from top to A. Each row builds left, stars, then right.

MethodIdeaBest for
Three inner loopsLeft letters, star loop, right mirrorLearning, interviews, exams
"*".repeat(stars)Same shape; fill the center in one callShorter 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

GoalPattern
Top for n rowsconst top = "A".charCodeAt(0) + rows - 1;
Walk end letter downfor (let i = top; i >= base; i--)
Left halffor (let j = base; j <= i; j++) line += String.fromCharCode(j);
Star countconst stars = 2 * (top - i);
Right halffor (let m = i; m >= base; m--) line += String.fromCharCode(m);
Star shortcutline += "*".repeat(stars);
Row widthAlways 2 * rows characters

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter and each *
console.log(line)Ends the current rowAfter left + stars + right

Append characters without a newline, then end the row once.

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.

Live result 5 rows · top E · width 10
ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********A

Worked Walkthrough — rows = 4 (top = D)

Trace each end letter i, the star count, and the finished row (width always 8).

iLeftStarsRightPrinted row
DABCD0DCBAABCDDCBA
CABC2CBAABC**CBA
BAB4BAAB****BA
AA6AA******A

Each row has 2n = 8 characters. Over n rows that is 2n² characters total — still O(n²) time.

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

How It Works

1. Set bounds. base is A; top is E (five rows).

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

How It Works

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

How It Works

1. Same left and right halves. Letter loops match Example 1.

2. Fill the center. "*".repeat(stars) appends the entire gap in one expression.

3. Print and advance. console.log still ends the row after all three parts.

Learn the three-loop version first (Examples 1–2); treat repeat as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong stars

Uneven gap / jagged width

Use 2 * (top - i). A factor of 1 makes an odd gap and breaks the fixed 2n width.

log early

Split across lines

Call console.log only after left, stars, and right finish. Logging earlier splits one row into pieces.

Reuse line

Growing leftovers

Reset line = "" at the start of each outer iteration, or previous characters stick around.

right start

Duplicate middle letter

Starting the right half at i doubles the peak when stars = 0 (ABCDEEDCBA). Starting at i - 1 yields ABCDEDCBA instead.

rows = 1

Single AA

Output is AA (left A + right A, zero stars) — a good sanity check.

rows > 26

Beyond Z

Clamp or reject — top leaves A–Z. Keep rows in 1…26 for alphabet demos.

Time and Space Complexity

ProgramTimeExtra space
Three loops (Examples 1–2)O(n²)O(n) for the current line string
"*".repeat (Example 3)O(n²)O(n) for the current line string

Each of n rows prints 2n characters, so total work is 2n² — still quadratic in n.

Key Takeaways

  • Rule: each row is left letters + even stars + mirrored right; width stays 2n.
  • Stars: 2 * (top - i) — always even, grows as letters shrink.
  • 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.

Next: Centered Alphabet Pyramid

Leading spaces plus continuous letters — A, then B C D, then E F G H I…

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