JavaScript Palindromic Alphabet Pyramid Pattern (Center A)

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

What Is This Pattern?

A palindromic alphabet pyramid grows left-aligned rows that read the same forward and back, mirroring around a single center A: A, BAB, CBABC, … EDCBABCDE.

Remember
Rule: left wing i..B, then right wing A..i (one center A)

A
BAB
CBABC
DCBABCD
EDCBABCDE     ← top = E

Contrast Program 18 (A, ABA, ABCBA — peak then down). Here the left wing descends to B first, then the right wing ascends from A.

How to Solve It

For each peak letter, append a descending wing that stops before A, then an ascending wing from A through the peak.

MethodIdeaBest for
Two inner loopsDescend i..B, then ascend A..iLearning, interviews, exams
Spaced variantSame wings; append ch + " " for readabilityWider demos in dense terminals

Pseudocode

Pseudocode
base = code of 'A'
top  = code of 'E'   // or chosen height
for i from base to top:          // row peak
    line = ""
    for j from i down to base+1: // left wing (stops before A)
        append fromCharCode(j)
    for j from base to i:        // right wing (includes A)
        append fromCharCode(j)
    print line

Cheat sheet

GoalPattern
Boundsconst base = "A".charCodeAt(0); const top = "E".charCodeAt(0);
Walk each peakfor (let i = base; i <= top; i++)
Left wingfor (let j = i; j > base; j--) line += String.fromCharCode(j);
Right wingfor (let j = base; j <= i; j++) line += String.fromCharCode(j);
Row length2 * (i - base) + 1 (odd: 1, 3, 5, …)
End the rowconsole.log(line);
Avoid double AUse j > base on the left wing — not >=

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter on both wings
console.log(line)Ends the current rowAfter both inner loops

Append without a newline, then end the row once.

Live Preview

Change the size (top letter) and the palindrome updates instantly — including peak letter and last-row length.

Whole numbers from 1 to 10. Size 5 means top letter E. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · top E · last len 9
A
BAB
CBABC
DCBABCD
EDCBABCDE

Worked Walkthrough — top = D

Trace left wing, right wing, and the joined palindrome for each peak.

PeakLeftRightPrinted rowLen
A(empty)AA1
BBABBAB3
CCBABCCBABC5
DDCBABCDDCBABCD7

Odd lengths 1 + 3 + … + (2n-1) = n² → O(n²) total characters.

JavaScript Programs

Three complete programs: fixed A–E, row-count prompt, and a spaced-letter style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed A–E

Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A, so A appears once.

JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);

for (let i = base; i <= top; i++) {
  let line = "";
  for (let j = i; j > base; j--) {
    line += String.fromCharCode(j);
  }
  for (let j = base; j <= i; j++) {
    line += String.fromCharCode(j);
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop picks the peak. i runs from A to E.

2. Left wing. Descend from i while j > base — stops at B, never prints a second A.

3. Right wing. Ascend from A through i. The center A comes only from this loop.

When i is C: left CB + right ABC → CBABC.

Example 2 — Row Count Input

Cap at 26 for A–Z. Validate parseInt(prompt()) with Number.isFinite.

JavaScript
let n = parseInt(prompt("Enter number of rows (1..26):"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a whole number >= 1.");
} else {
  if (n > 26) n = 26;
  const base = "A".charCodeAt(0);

  for (let r = 0; r < n; r++) {
    const peak = base + r;
    let line = "";

    for (let ch = peak; ch > base; ch--) {
      line += String.fromCharCode(ch);
    }
    for (let ch = base; ch <= peak; ch++) {
      line += String.fromCharCode(ch);
    }

    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Parse with parseInt, reject invalid values, clamp to 26.

2. Map row to peak. Row index r maps to peak "A".charCodeAt(0) + r. Same descend/ascend rules; only how many peaks change.

Example 3 — Spaced Letters

Same palindrome with a space after each letter for a wider look.

JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);

for (let i = base; i <= top; i++) {
  let line = "";
  for (let j = i; j > base; j--) {
    line += String.fromCharCode(j) + " ";
  }
  for (let j = base; j <= i; j++) {
    line += String.fromCharCode(j) + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same wing logic. Only the append format adds a trailing space after each letter.

2. Optional trim. Call line.trimEnd() before logging if you need a clean line end.

Edge Cases & Pitfalls

Check these before calling the solution done.

double A

j >= base on the left

Duplicates the center A (BAAB, CBABBC, …). Keep j > base.

vs Program 18

Wrong wing order

If rows look like ABCBA instead of CBABC, you used Program 18’s ascend-then-descend order.

centered?

No leading pads

This pattern is left-aligned. Add leading spaces (or see Program 16) for a centered pyramid.

log early

Split wings

Call console.log only after both inner loops finish.

n = 1

Single A

Output is just A (left wing empty) — a good sanity check.

Bad prompt

Validate and clamp

Reject NaN / non-positive values; cap at 26 so peaks stay in A–Z.

Time and Space Complexity

ProgramTimeExtra space
Two-wing loops (Examples 1–2)O(n²)O(n) for the current line string
Spaced style (Example 3)O(n²)O(n) for the current line string

Odd row lengths sum to n², so total printed characters (and work) are quadratic in the number of rows.

Key Takeaways

  • Rule: left wing i..B, then right wing A..i — one center A.
  • Stop early: left loop uses j > base, not >=.
  • Length: each row has 2*(i-A)+1 characters (odd widths).
  • Complexity: O(n²) from the odd-sum n².

One line: for each peak i, print i..B then A..i so the row mirrors around one A.

Frequently Asked Questions

The first loop appends descending letters from the row peak down to B. The second appends ascending from A through i. Together they mirror around a single A.
The ascending loop already appends A. Stopping the descending loop before A avoids duplicating the center character.
The descending loop stops at j > base, and the ascending loop starts at base. That prevents appending A twice at the join.
For row letter i, length is 2*(i-base)+1, so rows grow as 1, 3, 5, 7, 9 for A..E.
Change the outer loop upper bound from E to your target letter (like H), or use the row-count prompt() variant.
Program 18 typically builds A..peak then peak-1..A (ABA, ABCBA). This pattern descends to B first, then ascends A..i (BAB, CBABC), still mirroring around a single A.
O(n²) for n rows because total printed characters are 1+3+…+(2n-1) = n².
No — the classic version is left-aligned with no leading spaces. Add pad spaces (or see Program 16) if you want a centered pyramid.

Did you know?

Outer i runs A.. E. First inner loop appends the left wing (i down to B) by stopping at j > base. Second inner loop appends A.. i. Because the reverse loop stops before A, the center A is not duplicated.

Next: Sequential Decreasing Triangle

A running counter with shrinking rows — A B C D E, then F G H I, then J K L…

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