A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed right with leading spaces so every row shares the same right edge.
Remember
Rule: for row letter i from 'A' to top,
print (top - i) spaces, then A..i (width 2)
A
A B
A B C
A B C D
A B C D E ← top = 'E'
Unlike Program 22, letters restart from A on every row. Unlike Program 16, this only right-aligns — it does not fully center the pyramid.
Approach
How to Solve It
Two ways to emit the same shape — char-code loops with pads, then optionally rewrite with int row indexes.
Method
Idea
Best for
Char-code loops
Pad top..i+1; append A..i with padStart(2)
Learning, interviews, exams
Int row index
Pad n - row; append row letters from A
When you think in row numbers
Pseudocode
Pseudocode
for i from 'A' to top:
for j from top down to (i + 1):
append " "
for k from 'A' to i:
append k in width-2 field
print newline
Cheat sheet
Goal
Pattern
Walk each row
for (let i = base; i <= top; i++)
Leading spaces
for (let j = top; j > i; j--) line += " ";
Prefix letters
for (let k = base; k <= i; k++) line += String.fromCharCode(k).padStart(2, " ");
Append pads and letters without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the right-aligned pyramid updates instantly — including the letter total.
One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.
Live resultTop E · 5 rows · 15 letters
A
A B
A B C
A B C D
A B C D E
Trace
Worked Walkthrough — Top = E
Trace each row’s pad count, letter prefix, and printed line (letters use width-2 cells).
i
Pad spaces
Letters
Printed row
A
4
A
A
B
3
A B
A B
C
2
A B C
A B C
D
1
A B C D
A B C D
E
0
A B C D E
A B C D E
Pad count = top - i. Letters always restart at A. Total letters: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.
Code
JavaScript Programs
Three complete programs: fixed A–E, top-letter prompt, and an int-index rewrite. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed A–E
First append leading spaces, then append letters A..i using padStart(2, " ").
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = top; j > i; j--) {
line += " ";
}
for (let k = base; k <= i; k++) {
line += String.fromCharCode(k).padStart(2, " ");
}
console.log(line);
}
1. Outer loop picks the row end.i runs from A to E — one growing prefix per row.
2. Pad first. Append one space for each j from top down while j > i.
3. Restart letters. Append k from A through i with padStart(2, " ").
4. Break the line.console.log(line) after both inner loops starts the next row.
When i is C, two leading spaces print, then letters A B C.
Example 2 — Top Letter Input
The pattern prints up to that row. Prefer validating a single A–Z character.
JavaScript
const raw = (prompt("Enter top letter (like E):") || "").trim().toUpperCase();
const base = "A".charCodeAt(0);
if (!/^[A-Z]$/.test(raw)) {
console.log("Please enter a single letter A-Z.");
} else {
const top = raw.charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = top; j > i; j--) {
line += " ";
}
for (let k = base; k <= i; k++) {
line += String.fromCharCode(k).padStart(2, " ");
}
console.log(line);
}
}
1. Prompt and normalize. Trim, uppercase, and require a single A–Z letter.
2. Same pad + prefix core. Pad count is always top - i; only the shared top letter changes.
3. Same nested-loop core. Only the source of top changes — the print logic matches Example 1.
Example 3 — Int Row Index
Often clearer if you think in row numbers: pad n - row spaces, then append row letters from A.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
const n = top - base + 1;
for (let row = 1; row <= n; row++) {
let line = "";
for (let s = 0; s < n - row; s++) {
line += " ";
}
for (let L = 0; L < row; L++) {
line += String.fromCharCode(base + L).padStart(2, " ");
}
console.log(line);
}
1. Derive height.n = top - base + 1 is 5 when top is E.
2. Pad by row number. Row 1 pads n - 1 spaces; the last row pads 0.
3. Letters from A. Letter L is String.fromCharCode(base + L) — row 1 prints A; row 5 prints A..E.
Edge Cases & Pitfalls
Check these before calling the solution done.
Running counter
Continuous stream by mistake
If you keep a running code across rows, you get Program 22-style output. Restart the letter loop at base each row.
No pads
Left-aligned instead
Skipping the space loop prints a left-aligned triangle. Keep j from top down while j > i.
log inside
Broken outline
If console.log is inside either inner loop, each cell lands on its own line. Append with +=; log only after both loops.
top = A
Single letter
Output is just a width-2 A cell. Pad loop never runs. A good sanity check.
top < A
Empty output
Outer loop never runs if top < base. Validate A–Z before looping.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character prompts.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char-code loops (Examples 1–2)
O(n²)
O(n) for the current row string
Int row index (Example 3)
O(n²)
O(n) for the current row string
For n = top - 'A' + 1 rows, each row prints up to O(n) spaces plus O(n) letters. Letter total = 1 + 2 + … + n = n(n + 1)/2.
Remember
Key Takeaways
Rule: for row i, append top - i spaces, then letters A..i.
Restart A: every row begins the letter loop at base — not a running counter.
Break the row: call console.log(line) only after both inner loops.
Complexity:O(n²) time; O(n) space for the current row string.
One line: for each row letter i from A to top, append top - i spaces, then A..i with width 2, then console.log(line).
Frequently Asked Questions
The first inner loop appends (top - i) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the output matches the spaced layout (best viewed in a monospace font).
Program 22 uses a continuous counter across all rows. Program 27 restarts letters from A on every row and uses leading spaces to push rows to the right.
Remove the leading-space loop and append letters directly from A to the row letter.
line += builds pads and letters on the same row. console.log inside either inner loop would log one cell per line. Log once after both loops finish.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Use prompt(), trim, toUpperCase(), require a single A–Z character, and reject empty or multi-character input.
Yes. Pad n − row spaces, then append row letters as String.fromCharCode(base + L). Example 3 on this page shows that style.
🤔
Did you know?
Leading padding: for each row letter i, the loop appends top - i spaces. Then the letter loop appends A through i using padStart(2, " ") so columns look even in monospace output. The last row has no padding; all rows share the same right edge.