JavaScript Palindromic Alphabet Pyramid Pattern (Center A)
Beginner
8 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
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.
Method
Idea
Best for
Two inner loops
Descend i..B, then ascend A..i
Learning, interviews, exams
Spaced variant
Same wings; append ch + " " for readability
Wider 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
Goal
Pattern
Bounds
const base = "A".charCodeAt(0); const top = "E".charCodeAt(0);
Walk each peak
for (let i = base; i <= top; i++)
Left wing
for (let j = i; j > base; j--) line += String.fromCharCode(j);
Right wing
for (let j = base; j <= i; j++) line += String.fromCharCode(j);
Row length
2 * (i - base) + 1 (odd: 1, 3, 5, …)
End the row
console.log(line);
Avoid double A
Use j > base on the left wing — not >=
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch
Stays on the same row
Each letter on both wings
console.log(line)
Ends the current row
After both inner loops
Append without a newline, then end the row once.
Try it
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 result5 rows · top E · last len 9
A
BAB
CBABC
DCBABCD
EDCBABCDE
Trace
Worked Walkthrough — top = D
Trace left wing, right wing, and the joined palindrome for each peak.
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);
}
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);
}
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.