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.
Approach
How to Solve It
Either scan a fixed bottom width (spaces vs letters) or pad first, then print 2*row-1 letters.
Method
Idea
Best for
Width scan
Outer i += 2; inner scans bottom width — space or next letter
Learning the classic scan
Pad then letters
pad = width - (2*row-1), then print that many letters
Clearer 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
Goal
Pattern
Odd widths
for (let i = 1; i <= width; i += 2)
Bottom width from rows
const width = 2 * rows - 1;
Letters on row r
const letters = 2 * row - 1;
Leading spaces
const pad = width - letters;
Running letter
line += String.fromCharCode(code); code++;
End the row
console.log(line);
Stay in A–Z
Keep rows² ≤ 26 (max 5 rows)
Printing Letters vs Starting a New Line
API
Effect
Use for
line += " " / letter
Stays on the same row
Padding and each letter
console.log(line)
Ends the current row
After pad + letters finish
Append without a newline, then end the row once.
Try it
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 result3 rows · width 5 · 9 letters
A
B C D
E F G H I
Trace
Worked Walkthrough — rows = 3 (width = 5)
Trace each row’s pad, letter count, and the running alphabet.
row
Pad
Letters
Printed row
1
4
A
A
2
2
B C D
B C D
3
0
E F G H I
E 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²).
Code
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);
}
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.