Shape Rule
Right-aligned reverse
Growing reverse suffixes sit on the right of a fixed width.

Each row is a reverse suffix (A, BA, CBA, …) padded on the left so the letters line up on the right in a fixed-width column. This is the same code > i idea as the left half of Program 19, but without the second mirror loop. Compare Program 2 (reverse, left-aligned). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Right-aligned reverse
Growing reverse suffixes sit on the right of a fixed width.
Row peak
i walks A..top so the visible suffix grows each row.
Fixed width
code always walks top down to A for every row.
code > i
Print a space while above the peak; then print letters.
Top letter
Pick A–J and draw the right-aligned pyramid instantly.
Complexity
n rows × n columns per fixed-width scan.
A right-aligned reverse alphabet pyramid keeps every row the same width and fills the left with spaces until the reverse suffix begins — so A, BA, CBA, … line up on the right edge.
In JavaScript you solve it with nested loops and ord/chr: outer i grows the peak, inner code scans top..A, and code > i decides space vs letter.
It combines three beginner skills: fixed-width scans, leading-space padding, and descending letter order — the same toolkit used for many right-aligned pyramids.
Every row scans top..A columns.
code > i pads until the suffix starts.
Descending code prints BA, CBA, DCBA…
Outer i from A to top lengthens the suffix.
In short: for each peak i, scan top..A - append a space while code > i, otherwise append String.fromCharCode(code), then call console.log(line).
Given a top letter (like E), print a right-aligned pyramid of reverse alphabet suffixes.
# Classic sample (top = E; leading spaces matter)
# A
# BA
# CBA
# DCBA
# EDCBA | Item | Type | Description |
|---|---|---|
top | str / int | Highest letter (e.g. E) or top.charCodeAt(0). Line width = top - 'A' + 1. |
| Printed output | text | Right-aligned reverse suffixes with leading spaces. |
for i from base to top:
line = ""
for code from top down to base:
if code > i: line += " "
else: line += String.fromCharCode(code)
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Fixed-width scan | Space-or-letter in each column | Matching this classic sample |
| Explicit pad + suffix | Print spaces, then i..A reverse | Clearer reading / teaching rewrite |
| Goal | Pattern |
|---|---|
| Row peaks | for (let i = base; i <= top; i++) |
| Fixed scan | for (let code = top; code >= base; code--) |
| Pad vs letter | line += (code > i ? " " : String.fromCharCode(code)) |
| End the row | console.log(line) |
| Left-aligned reverse | See Program 2 |
| Add right mirror | See Program 19 |
Same fixed-width row — different roles on each column.
code > iLeading pads that create right alignment
code <= iReverse suffix letters for the current peak
E..AInner direction makes BA, CBA, DCBA…
breakEnds the row after the full width scan
Reach for this when teaching right alignment with reverse letter fills.
Keep one half of the dual scan — the pad-and-suffix idea alone.
Practice leading spaces on a fixed-width console line.
Same reverse letters; left-aligned vs right-aligned layout.
Padding intuition helps when you later center rows.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one condition (code > i) turns a flat reverse scan into a right-aligned pyramid.
Enter a top letter from A to J and draw the right-aligned reverse pyramid in the browser.
Three complete JavaScript programs - fixed A–E, user-chosen top letter, and an explicit pad-then-suffix rewrite. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five right-aligned reverse rows with a fixed-width scan.
EOuter i is the row peak. Inner code sweeps E down to A and appends a space until it reaches i.
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);
} When i = "C".charCodeAt(0), columns E and D append spaces; then C, B, A append → ··CBA. When i = "E".charCodeAt(0), every column is a letter → EDCBA.
Let the user choose the last letter.
The pattern keeps the line width fixed to the chosen top letter. Prefer validating a single A–Z character from prompt().trim().toUpperCase() in real apps.
const raw = (prompt("Enter the top letter (like E):") || "").trim().toUpperCase();
const top = raw ? raw.charCodeAt(0) : "E".charCodeAt(0);
const base = "A".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);
} Same code > i rule; only the shared bounds follow top. With top = "D".charCodeAt(0) you get a 4-column right-aligned pyramid.
Same shape with separate pad and suffix loops.
Often clearer to read: append leading spaces first, then letters from the peak down to A.
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);
} Peak i needs i - base + 1 letters and width - letters leading spaces. The suffix loop appends String.fromCharCode(i) down to A - same visual pyramid as the scan version.
i moves from "A".charCodeAt(0) to top, increasing the visible suffix each time.
code runs from top down to "A".charCodeAt(0), giving a fixed-width line.
If code > i append a space; otherwise append String.fromCharCode(code). Leading spaces push the suffix to the right edge.
console.log(line) ends the row so the next peak starts fresh.
For n letters, total work is O(n²) time, O(1) extra memory.
ETrace each peak i and how many pads vs letters print.
i | Leading spaces | Suffix | Printed row |
|---|---|---|---|
A | 4 | A | ····A |
B | 3 | BA | ···BA |
C | 2 | CBA | ··CBA |
D | 1 | DCBA | ·DCBA |
E | 0 | EDCBA | EDCBA |
Pad count is top - i (as char distance). Each row still scans 5 columns.
Where this right-aligned reverse pyramid shows up beyond the homework prompt.
Clearest alphabet demo of leading spaces on a fixed width.
Example: print . instead of spaces while debugging.
Same reverse letters — left-aligned vs right-aligned.
Example: print both for top E side by side.
This scan is the left half of the mirrored-gap pattern.
Example: add a right mirror pass next.
Teach pad count separately from the reverse suffix (Example 3).
Example: compare scan vs pad+suffix outputs.
Fixed-width scans make O(n²) easy to count.
Example: 5 rows × 5 columns = 25 writes.
Practice reading and validating a single top letter.
Example: reject empty strings and non A–Z input.
Pro Tip: say “pad while above the peak, then print reverse letters” before coding — that story prevents flipped alignment.
Why this pattern earns a spot after left-aligned reverse triangles.
Missing pads or a flipped inner loop show up as a broken pyramid immediately.
Fixed-width scan or explicit pad/suffix loops teach the same shape.
Master one half before adding the mirrored right ramp.
Streaming output needs no storage beyond loop variables.
Pro Tip: learn the classic scan version first; treat the explicit pad/suffix rewrite as a clarity upgrade afterward.
Small habits that keep right-aligned reverse pyramids clean.
Always scan top..A so shorter suffixes stay right-aligned.
Tabs change width by editor settings and ruin alignment.
Require a single A–Z character; empty input breaks top_ch[0].
Temporarily print . instead of spaces to count the gap.
Trace two spaces then CBA on paper before coding larger tops.
Pro Tip: if letters sit on the left with trailing spaces, you almost certainly flipped the code > i condition.
Mistakes that commonly break right-aligned reverse alphabet pyramids.
Using code < i for spaces left-aligns or garbles the suffix.
→ Print a space when code > i.
Going A..top prints forward letters (AB, ABC) instead of reverse suffixes.
→ Keep code descending from top to A.
Alignment depends on the editor’s tab size.
→ Always print a single space character.
top_ch[0]Empty or multi-character input can throw or pick the wrong char.
→ Read a string, check length, take [0], validate A–Z.
Breaks the row into one character per line.
→ Call console.log(line) only after the full width finishes.
Check these inputs before calling the solution done.
Output is just A (no pads).
Five rows ending in EDCBA.
Four columns; last row DCBA.
Reject or cap so indices stay in A–Z.
top_ch[0] fails on empty tokens — validate first.
Same loops; only the pad character changes.
Try these variations to lock in the pattern.
code > i creates leading spaces; descending code creates reverse suffixes.Quick Takeaway: scan top..A, pad while above the peak, print the reverse suffix, then break the line — that is the whole pyramid.
| Program | Time | Extra space |
|---|---|---|
| Fixed-width scan (Examples 1–2) | O(n²) | O(1) |
| Explicit pad + suffix (Example 3) | O(n²) | O(1) |
With n = top − ‘A’ + 1, each of n rows scans n columns (or pads + letters totaling n), so work is O(n²).
The right-aligned reverse alphabet pyramid is a small nested-loop exercise with lasting payoff: fixed-width scans, leading-space padding, and descending letter fills. Master the classic ····A…EDCBA sample, then try user input and the explicit pad rewrite.
Practice the three examples above, then continue to Program 21’s diamond alphabet pattern with alternating stars.
Scan top..A, append spaces while code > i, append letters otherwise, and break only after the scan.
code > icode > i unless you want left alignmentconsole.log(line) inside the column scantop_ch[0]Print the right-aligned reverse alphabet pyramid the beginner-friendly way.
Pad + reverse suffix
DefinitionAlways top..A
Codecode > i → space
CodeEnds each scan
I/OO(n²) time
AnalysisEach 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.
Next up: diamond-style alphabet rows that alternate letters and stars.
12 people found this page helpful