JavaScript Alphabet Pyramid Pattern (Centered)

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

What Is This Pattern?

A centered alphabet pyramid prints odd-width rows of consecutive letters with leading spaces so each row sits under the widest bottom line. Letters do not reset — one running counter continues across rows.

Remember
Rule: odd widths 1, 3, 5…; pad left; letters keep advancing

    A
  B C D
E F G H I     ← 3 rows (bottom width 5)

Combines the odd-step idea from Program 14 with the continuous counter from Program 13, plus centering like a star pyramid.

How to Solve It

Either scan a fixed bottom width (spaces vs letters) or pad first, then print 2*row-1 letters.

MethodIdeaBest for
Width scanOuter i += 2; inner scans bottom width — space or next letterLearning the classic scan
Pad then letterspad = width - (2*row-1), then print that many lettersClearer separation of concerns

Pseudocode

Pseudocode
width = 2 * rows - 1
code = code of 'A'
for row from 1 to rows:
    letters = 2 * row - 1
    pad = width - letters
    line = ""
    append pad spaces to line
    for L from 1 to letters:
        append fromCharCode(code) (+ space if not last)
        code = code + 1
    print line

Cheat sheet

GoalPattern
Odd widthsfor (let i = 1; i <= width; i += 2)
Bottom width from rowsconst width = 2 * rows - 1;
Letters on row rconst letters = 2 * row - 1;
Leading spacesconst pad = width - letters;
Running letterline += String.fromCharCode(code); code++;
End the rowconsole.log(line);
Stay in A–ZKeep rows² ≤ 26 (max 5 rows)

Printing Letters vs Starting a New Line

APIEffectUse for
line += " " / letterStays on the same rowPadding and each letter
console.log(line)Ends the current rowAfter pad + letters finish

Append without a newline, then end the row once.

Live Preview

Change the row count and the centered pyramid updates instantly — including bottom width and letter total.

Whole numbers from 1 to 5 (keeps letters in A–Z). Tap a chip or type a value — the preview redraws as you go.

Live result 3 rows · width 5 · 9 letters
    A
  B C D
E F G H I

Worked Walkthrough — rows = 3 (width = 5)

Trace each row’s pad, letter count, and the running alphabet.

rowPadLettersPrinted row
14AA
22B C DB C D
30E F G H IE F G H I

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

JavaScript Programs

Three complete programs: fixed bottom width, odd-width prompt, and an explicit pad-then-letters style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed bottom width 5

Hard-coded bounds with a width scan — ideal for first demos and screenshots.

JavaScript
let code = "A".charCodeAt(0);
const width = 5;

for (let i = 1; i <= width; i += 2) {
  let line = "";
  for (let j = width; j >= 1; j--) {
    if (j > i) {
      line += " ";
    } else {
      line += String.fromCharCode(code) + " ";
      code++;
    }
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Start the running code once. code begins at A and is never reset between rows.

2. Outer loop steps odd widths. i takes 1, 3, 5 — how many letters appear on the row.

3. Inner loop scans the bottom width. While j > i, append a space; otherwise append the next letter (plus a separator space) and code++.

4. Break the line. console.log(line) after the scan finishes.

Example 2 — Odd Width Input

Read the bottom width at runtime. Require an odd positive width so rows stay 1, 3, 5…

JavaScript
let width = parseInt(prompt("Enter the bottom width (odd number):"), 10);
let code = "A".charCodeAt(0);

if (!Number.isFinite(width) || width < 1) {
  console.log("Please enter a positive odd width (e.g. 5).");
} else {
  if (width % 2 === 0) width += 1;
  if (width > 9) width = 9; // rows = 5 keeps letters in A-Y

  for (let i = 1; i <= width; i += 2) {
    let line = "";
    for (let j = width; j >= 1; j--) {
      if (j > i) {
        line += " ";
      } else {
        line += String.fromCharCode(code) + " ";
        code++;
      }
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Ask for the bottom width, then convert with parseInt(..., 10).

2. Force an odd width. If the user typed an even number, bump by one so the pyramid stays odd-stepped.

3. Same scan core. Only the bound width changes — the print logic matches Example 1.

Example 3 — Pad Spaces, Then Letters

Often clearer: append leading spaces first, then the odd letter count for that row.

JavaScript
const rows = 3;
const width = 2 * rows - 1;
let code = "A".charCodeAt(0);

for (let row = 1; row <= rows; row++) {
  const letters = 2 * row - 1;
  const pad = width - letters;
  let line = "";

  for (let s = 0; s < pad; s++) {
    line += " ";
  }

  for (let L = 0; L < letters; L++) {
    line += String.fromCharCode(code);
    if (L < letters - 1) line += " ";
    code++;
  }

  console.log(line);
}
Try It Yourself

How It Works

1. Derive width from rows. width = 2 * rows - 1 is the odd bottom length.

2. Pad, then letters. Row r needs 2r-1 letters and width - letters leading spaces.

3. Keep advancing code. Same continuous alphabet as Examples 1–2; only the loop structure is clearer.

Edge Cases & Pitfalls

Check these before calling the solution done.

even width

Broken odd steps

Bottom width must be odd (1, 3, 5…). An even width misaligns the final row with the i += 2 sequence.

reset code

Looks like Program 14

If you reset to A each row, you get A / ABC / ABCDE instead of the continuous A / B C D / E F G H I sequence.

no pad

Left-aligned only

Without leading spaces the letters still grow oddly, but the shape is no longer a centered pyramid.

log inside

Column of characters

Call console.log only after pad and letters finish. Logging inside the inner loop splits one row across many lines.

past Z

Beyond the alphabet

Letter total is r². Keep r ≤ 5 for A–Z only (25 letters), or stop when code > "Z".charCodeAt(0).

rows = 1

Single A

Output is just A (no padding) — a good sanity check.

Time and Space Complexity

ProgramTimeExtra space
Width scan (Examples 1–2)O(r²)O(r) for the current line string
Pad then letters (Example 3)O(r²)O(r) for the current line string

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

Key Takeaways

  • Rule: odd widths 1, 3, 5…; pad left; letters advance continuously.
  • Two pieces: leading spaces for centering + a running code++.
  • Break the row: call console.log(line) only after pad and letters finish.
  • Complexity: O(r²) time because letter totals form a perfect square.

One line: for each odd width, pad left, print the next letters with code++, then console.log.

Frequently Asked Questions

Leading spaces (or a scan that appends spaces while outside the current odd width) shift each row right so shorter lines sit under the widest bottom row.
The outer loop increases by 2 (1, 3, 5) via for (let i = 1; i <= width; i += 2), so each row prints an odd number of letters, forming a pyramid shape.
Increase the maximum odd width (or the row count). Rows grow as 1, 3, 5, 7… Letter totals form a perfect square: 1+3+…+(2r−1)=r².
They shift each row to the right so the odd-width lines are centered under the widest line.
The separator makes columns easier to see. For a compact output, append String.fromCharCode(code) without the extra space.
line += appends spaces or letters on the same row. console.log(line) ends the current line. Build the full row first; log once after the inner work.
O(r²) for r rows because letter totals equal r² and each row also scans padding proportional to the bottom width.
Use parseInt with Number.isFinite after prompt(), require an odd positive width (1, 3, 5, …), and keep r² ≤ 26 if you want only A–Z letters.

Did you know?

This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids). Letters flow continuously via one counter — A, then B C D, then E F G H I — while leading spaces keep each row aligned under the widest line.

Next: Reverse Alphabet with Diagonal Star

Countdown letters E…A with a moving * on the diagonal — EDCB*, EDC*A…

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