JavaScript Palindromic Alphabet Pyramid Pattern

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

What Is This Pattern?

A palindromic alphabet pyramid grows one peak letter per row and mirrors the left half so each full line reads the same both ways — without printing the peak twice.

Remember
Rule: A..peak, then (peak-1)..A

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA     ← 5 rows

Contrast Program 1 (left half only: A, AB, ABC…). Here you add a reverse pass that starts at peak - 1 so the center letter appears once.

How to Solve It

For each row: print up to the peak, then mirror back down. Optionally add leading spaces to center the pyramid.

MethodIdeaBest for
Up then downA..peak, then (peak-1)..ALearning, interviews, exams
CenteredSame palindrome + n - r - 1 leading spacesPyramid look (like Program 16)

Pseudocode

Pseudocode
base = code of 'A'
for r from 0 to n - 1:
    peak = base + r
    line = ""
    for code from base to peak:
        append fromCharCode(code)
    for code from peak - 1 down to base:
        append fromCharCode(code)
    print line

Cheat sheet

GoalPattern
Walk rowsfor (let r = 0; r < n; r++)
Peak for row rconst peak = "A".charCodeAt(0) + r;
Left halffor (let code = base; code <= peak; code++) line += String.fromCharCode(code);
Right halffor (let code = peak - 1; code >= base; code--) line += String.fromCharCode(code);
Avoid double peakStart mirror at peak - 1, not peak
End the rowconsole.log(line);
Row length2*(r+1) - 1 letters (odd widths)

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter in both halves
console.log(line)Ends the current rowAfter both halves finish

Append without a newline, then end the row once.

Live Preview

Change the row count and the palindrome pyramid updates instantly — including peak letter and perfect-square total.

Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · peak E · 25 letters
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

Worked Walkthrough — n = 4

Trace each row’s peak and the up / down halves.

rPeakUpDownPrinted row
0AA(empty)A
1BABAABA
2CABCBAABCBA
3DABCDCBAABCDCBA

Total letters: 1 + 3 + 5 + 7 = 16 = 4². Odd widths sum to a perfect square — that is why time is O(n²).

JavaScript Programs

Three complete programs: fixed A–E with a string index, prompt row count, and a centered variant. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed A–E (string)

Append the forward part A..i, then append back from i-1..A using an alphabet string.

JavaScript
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (let i = 0; i < 5; i++) {
  let line = "";
  for (let j = 0; j <= i; j++) {
    line += alpha[j];
  }
  for (let k = i - 1; k >= 0; k--) {
    line += alpha[k];
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop picks the peak index. i runs 0..4 so peaks are A..E.

2. Forward half. Append alpha[0] through alpha[i] (A through the peak).

3. Mirror half. Append from i - 1 down to 0 — skipping the peak so it is not duplicated.

4. Break the line. console.log(line) after both halves finish.

When i = 2: forward ABC, reverse BA → ABCBA. Starting reverse at i would wrongly print ABCCBA.

Example 2 — User Input Version

Read the row count at runtime. Uses charCodeAt/fromCharCode and clamps to 26 for A–Z peaks.

JavaScript
let n = parseInt(prompt("Enter number of rows (like 5):"), 10);
const base = "A".charCodeAt(0);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a whole number of rows >= 1.");
} else {
  if (n > 26) n = 26;

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

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

    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 clamp. Reject NaN or non-positive values; cap at 26 so peaks stay in A–Z.

3. Same up/down core. Row r peaks at base + r; mirror starts at peak - 1.

Example 3 — Centered Palindrome Pyramid

Add leading spaces so shorter rows sit under the widest row (same centering idea as Program 16).

JavaScript
const n = 5;
const base = "A".charCodeAt(0);

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

  for (let s = 0; s < n - r - 1; s++) {
    line += " ";
  }

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

  console.log(line);
}
Try It Yourself

How It Works

1. Pad first. Append n - r - 1 spaces so shorter rows sit under the widest row.

2. Same palindrome. Up to peak, then down from peak - 1 — letter logic unchanged.

3. Print and advance. console.log ends the row after pad + both halves.

Edge Cases & Pitfalls

Check these before calling the solution done.

double peak

ABCCBA instead of ABCBA

Start the mirror at peak - 1 (or i - 1). Starting at peak prints the center letter twice.

log early

Split halves

Call console.log only after both halves finish. Logging between up and down splits one palindrome across two lines.

Reuse line

Growing leftovers

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

n = 1

Single A

Output is just A — the reverse loop does not run (peak - 1 < base). A good sanity check.

n > 26

Beyond Z

Clamp or reject — peaks leave A–Z. Keep n in 1…26 for alphabet demos.

Bad prompt

Use Number.isFinite

Bare parseInt(prompt()) yields NaN on letters — validate before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Left-aligned (Examples 1–2)O(n²)O(n) for the current line string
Centered (Example 3)O(n²)O(n) for the current line string

Total letters logged = 1 + 3 + … + (2n-1) = n², so time is quadratic in the number of rows.

Key Takeaways

  • Rule: each row is A..peak then (peak-1)..A — a palindrome.
  • Skip the center: start the mirror at peak - 1, not peak.
  • Break the row: call console.log(line) only after both halves.
  • Complexity: O(n²) time because letter totals form a perfect square.

One line: for each peak, print A through peak, then back from peak - 1 to A.

Frequently Asked Questions

The peak letter was already printed by the first loop. Starting the mirror at peak − 1 avoids printing the center letter twice.
Append A…peak, then append (peak−1)…A so the center letter is not duplicated.
Row r prints 2r−1 letters. Over n rows the total is 1+3+…+(2n−1)=n².
Yes. Append each letter followed by a space in both halves, and update the sample output accordingly.
line += String.fromCharCode(code) stays on the same row for each letter. console.log(line) ends the row after both halves finish.
Program 1 prints only the left half (A, AB, ABC…). This pattern mirrors back down so each full row is a palindrome.
O(n²) for n rows because each row prints O(n) characters and the sum of odd lengths is n².
Use parseInt with Number.isFinite after prompt(), require n ≥ 1, and cap at 26 so peaks stay within A–Z.

Did you know?

Each row prints A up to the row peak, then prints back down starting from peak - 1 so the middle letter appears only once. Row length is 2r - 1 for row r, so the total characters over n rows is n².

Next: Mirrored Alphabet with Spaces

Two fixed-width passes per row — letters grow while the gap shrinks to ABCDEEDCBA.

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