JavaScript Reverse Alphabet Pyramid Pattern (Build to A)
Beginner
8 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned reverse alphabet pyramid keeps a fixed line width and prints a reverse suffix on the right: leading spaces shrink until the last row is a full reverse run such as EDCBA.
Remember
Rule: spaces while code > peak, then peak..A
A
BA
CBA
DCBA
EDCBA ← top = E
Contrast Program 2 (left-aligned reverse prefixes: E, ED, EDC…). Here each row is a reverse suffix pushed right by leading spaces.
Approach
How to Solve It
Either scan the full width with a space-or-letter condition, or append pads first and then the reverse suffix.
Method
Idea
Best for
Fixed-width scan
Walk top…A; space if code > i, else letter
Learning the classic column rule
Pad + reverse suffix
Append width - letters spaces, then i down to A
Clearer separation of concerns
Pseudocode
Pseudocode
base = code of 'A'
top = code of 'E' // or chosen top
width = top - base + 1
for i from base to top: // row peak
line = ""
pad = width - (i - base + 1)
append pad spaces
for ch from i down to base: // reverse suffix
append fromCharCode(ch)
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++)
Scan columns
for (let code = top; code >= base; code--)
Space or letter
line += (code > i) ? " " : String.fromCharCode(code);
Pad count
width - (i - base + 1)
End the row
console.log(line);
Row width
Always top - base + 1 characters
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch / " "
Stays on the same row
Each space and each letter
console.log(line)
Ends the current row
After the full-width scan / pad + suffix
Append without a newline, then end the row once.
Try it
Live Preview
Change the size (top letter) and the right-aligned reverse pyramid updates instantly — including pad count on the first row.
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 letters · top E · width 5
A
BA
CBA
DCBA
EDCBA
Trace
Worked Walkthrough — top = D
Width is 4. Trace pads and the reverse suffix for each peak.
Peak
Pad
Suffix
Printed row
A
3
A
A
B
2
BA
BA
C
1
CBA
CBA
D
0
DCBA
DCBA
Each of n rows prints n characters → O(n²) total work.
Code
JavaScript Programs
Three complete programs: fixed A–E scan, top-letter prompt, and an explicit pad + suffix style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed Top E
Outer i is the row peak. Inner code sweeps E down to A and appends a space until it reaches i.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let code = top; code >= base; code--) {
line += (code > i) ? " " : String.fromCharCode(code);
}
console.log(line);
}
2. Inner scan is fixed width.code always walks E down to A — five columns every row.
3. Space or letter. While code > i, append a space; otherwise append fromCharCode(code).
4. Break the line.console.log(line) after the scan finishes.
When i is C: spaces for E and D, then CBA. When i is E: no spaces → EDCBA.
Example 2 — Top Letter Input
The pattern keeps the line width fixed to the chosen top letter. Validate a single A–Z character.
JavaScript
const raw = (prompt("Enter the 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 code = top; code >= base; code--) {
line += (code > i) ? " " : String.fromCharCode(code);
}
console.log(line);
}
}
1. Prompt and validate. Trim, uppercase, and require a single A–Z letter.
2. Derive bounds.top sets both the last peak and the scan start. With D you get a 4-column pyramid.
3. Same code > i core. Only the shared bounds follow top — the print logic matches Example 1.
Example 3 — Pad Spaces, Then Reverse Suffix
Often clearer: append leading spaces first, then letters from the peak down to A.
JavaScript
const top = "E".charCodeAt(0);
const base = "A".charCodeAt(0);
const width = top - base + 1;
for (let i = base; i <= top; i++) {
const letters = i - base + 1;
const pad = width - letters;
let line = "";
for (let p = 0; p < pad; p++) {
line += " ";
}
for (let ch = i; ch >= base; ch--) {
line += String.fromCharCode(ch);
}
console.log(line);
}
1. Count letters and pads. Peak i needs i - base + 1 letters and width - letters leading spaces.
2. Pad first. Append that many spaces so the suffix sits on the right.
3. Reverse suffix. Append i down to A — same visual pyramid as the scan version.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong compare
code >= i vs code > i
Spaces must stop when code reaches the peak. Using >= skips the peak letter itself.
ascending scan
Forward letters
Scanning A…E upward prints forward suffixes, not reverse. Keep code-- from top.
log early
Broken columns
Call console.log only after the full-width scan (or pad + suffix) finishes.
vs Program 2
Left vs right
Program 2 is left-aligned growing prefixes (E, ED…). This pattern is right-aligned reverse suffixes with pads.
top = A
Single A
Output is just A (no pads) — a good sanity check.
Bad prompt
Validate one letter
Reject empty strings and multi-character input before computing top.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed-width scan (Examples 1–2)
O(n²)
O(n) for the current line string
Pad + suffix (Example 3)
O(n²)
O(n) for the current line string
Each of n rows prints n characters (spaces + letters), so total work is quadratic in n.
Remember
Key Takeaways
Rule: spaces while code > peak, then reverse suffix peak..A.
Width: every row has top - base + 1 columns.
Break the row: call console.log(line) only after pads and letters finish.
Complexity:O(n²) from n rows × n columns.
One line: for each peak i, pad with spaces, then print i down to A.
Frequently Asked Questions
code walks from E down to A. While code is above the current row peak i, append spaces; once code reaches i and below, append letters. That pushes the visible suffix (like CBA) to the right of a fixed-width line.
Because we append leading spaces for columns where code > i. That pushes the letters to the right and forms a right-aligned pyramid.
Descending code prints letters in reverse order (BA, CBA, DCBA). If code went upward, you would get AB, ABC, ABCD instead.
Update the loop bounds so the outer loop runs up to 'H'.charCodeAt(0) and the inner scan starts from 'H'.charCodeAt(0) down to 'A'.charCodeAt(0).
line += ch or line += ' ' stays on the same row for each cell. console.log(line) ends the row after the fixed-width scan finishes.
Program 2 prints reverse prefixes left-aligned (E, ED, EDC…). This pattern prints reverse suffixes right-aligned with leading spaces (A, BA, CBA…).
O(n²) for n letters because there are n rows and each row scans n positions.
Use prompt().trim().toUpperCase(), require a single A–Z character, and reject empty tokens. Cap at Z if you only want alphabetic ranges.
🤔
Did you know?
Each line has fixed width (five columns for A…E). Scanning from top down to A, letters above the row peak turn into spaces, so the visible suffix (CBA, DCBA, …) sits on the right.