Shape Rule
Reverse slices
Rows grow: E, then E D, … E D C B A.

Companion to Program 22: same fixed-width grid (two spaces for padding + String.fromCharCode(j).padStart(2, " ") for letters), but each row prints a reverse alphabet slice from the top letter down to the current row letter. Use a monospace terminal so columns stay aligned. Compare Program 20 (right-aligned reverse without fixed-width cells). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Reverse slices
Rows grow: E, then E D, … E D C B A.
Pad, then letters
Empty cells first; reverse slice second.
Width 2
Pad with " "; append letters with String.fromCharCode(j).padStart(2, " ").
E → A
Outer i descends from the top letter to A.
Top letter
Pick a top letter (A–F in the preview) and draw.
Complexity
n rows × O(n) pad + letter work.
A right-aligned reverse alphabet pyramid prints a growing reverse slice of the alphabet on each row, right-aligned with matching pad and letter cell widths.
In JavaScript you solve it with a descending outer loop over letter codes and two inner loops: padding, then letters from the top letter down to the row letter.
It shows how reverse ranges, padding counts, and format widths work together — a step beyond continuous k++ streams.
Each row prints top..i.
Pads shrink as rows grow.
" " matches ch.padStart(2, " ").
Slice per row, not a stream.
In short: for each row letter i from top down to A, append pads for A..(i-1), then letters top..i with width 2, then call console.log(line).
Given a top letter (or fixed E), print a right-aligned pyramid of reverse alphabet slices ending at A.
// Five rows (monospace; each cell is width 2)
// E
// E D
// E D C
// E D C B
// E D C B A | Item | Type | Description |
|---|---|---|
top | char | Highest letter (e.g. E). Rows run from top down to A. |
| Printed output | text | Right-aligned reverse slices in fixed-width cells. |
for i from top down to base:
line = ""
for j from base to (i - 1):
line += " "
for j from top down to i:
line += String.fromCharCode(j).padStart(2, " ")
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Char loops (classic) | Pad A..(i-1); letters top..i | Matching this sample |
| Int row index | n = top-'A'+1; pad n-row; letters by index | When you prefer int counters |
| Goal | Pattern |
|---|---|
| Outer rows | for (let i = top; i >= base; i--) |
| Pad cells | for (let j = base; j < i; j++) line += " " |
| Letter slice | for (let j = top; j >= i; j--) line += String.fromCharCode(j).padStart(2, " ") |
| End row | console.log(line) |
| Sequential stream | See Program 22 (k++) |
Same row — three roles that build the reverse pyramid.
pad2-column empty cells for right alignment
sliceReverse letters from top down to row letter
growEach row adds one more letter on the left of the slice
breakEnds the row after pads + letters
Reach for this when teaching reverse ranges with fixed-width alignment.
Same grid idea; reverse slices instead of a continuous stream.
Practice looping on char instead of only int.
Similar reverse right-align idea; this page stresses width-2 cells.
Match pad string length to String.fromCharCode(j).padStart(2, " ") exactly.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: separate pad and reverse-letter loops make right-aligned reverse pyramids easy to read and debug.
Choose a top letter from A to F and draw the right-aligned reverse pyramid in the browser (monospace cells).
Three complete JavaScript programs - fixed A–E, user-chosen top letter, and an int-index 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 from E down to A.
First append padding pairs, then append letters from E down to the current row letter.
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = top; i >= base; i--) {
let line = "";
for (let j = base; j < i; j++) {
line += " ";
}
for (let j = top; j >= i; j--) {
line += String.fromCharCode(j).padStart(2, " ");
}
console.log(line);
} When i = "C".charCodeAt(0), pads run for A and B (two cells), then letters append E D C. Pads shrink and the reverse slice grows until the bottom row is E D C B A.
Let the user choose the starting (top) letter.
The pattern prints rows from the chosen top letter down to A. Use prompt().trim().toUpperCase() and validate a single A–Z character in real apps.
const raw = (prompt("Enter the top letter (like E):") || "").trim().toUpperCase();
if (raw.length !== 1 || raw < "A" || raw > "Z") {
console.log("Please enter a single letter.");
} else {
const top = raw.charCodeAt(0);
const base = "A".charCodeAt(0);
for (let i = top; i >= base; i--) {
let line = "";
for (let j = base; j < i; j++) {
line += " ";
}
for (let j = top; j >= i; j--) {
line += String.fromCharCode(j).padStart(2, " ");
}
console.log(line);
}
} Same pad/letter rules; only the shared top letter changes. Both the outer start and the letter-loop start use top.
Same shape with integer row and column indexes.
Often clearer if you think in row numbers: pad n - row cells, then append row letters from the top down.
const top = "E".charCodeAt(0);
const base = "A".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 k = 0; k < row; k++) {
line += String.fromCharCode(top - k).padStart(2, " ");
}
console.log(line);
} Pad count is n - row; letter index k uses String.fromCharCode(top - k). Same visual pyramid as the letter-code scan - only the indexing style changes.
Top row has one letter (E); each next row grows by one letter until E D C B A.
For j = A..(i-1) we append " ". When i is E we append 4 padding cells; when i is A, we append 0.
Second inner loop appends j = E..i, using padStart(2, " ") to keep each letter 2 columns wide.
console.log(line) ends the row so the next lower i can grow the slice.
Two inner loops keep the block aligned while it grows by one letter per row — O(n²) time.
Trace each row’s pads, reverse slice, and printed line.
i | Pad cells | Letters | Printed row |
|---|---|---|---|
E | 4 | E | ········E |
D | 3 | E D | ······E D |
C | 2 | E D C | ····E D C |
B | 1 | E D C B | ··E D C B |
A | 0 | E D C B A | E D C B A |
Row count = top - 'A' + 1 (5 for E). Each cell is 2 columns wide.
Where this reverse right-aligned pyramid shows up beyond the homework prompt.
Clearest demo of printing top..i each row.
Example: swap descending for ascending and compare.
Same fixed-width grid — stream vs reverse slice.
Example: print both for 5 rows side by side.
Outer and inner loops over char ranges.
Example: rewrite with int indexes (Example 3).
Match pad string length to letter field width.
Example: try one-space pads and watch columns break.
Growing reverse slices make O(n²) easy to see.
Example: 5 rows print 1+2+3+4+5 letter cells.
Reverse wings lead naturally into palindromic pyramids.
Example: continue to Program 24.
Pro Tip: say “pads first, then top down to the row letter” before coding — that story prevents confusing this with Program 22’s stream.
Why this pattern earns a spot after sequential right-aligned triangles.
Wrong pad count or letter direction shows up immediately.
Char loops or int indexes teach the same shape.
A natural place to learn descending char loops.
Streaming output needs no storage beyond loop variables.
Pro Tip: learn the classic char-loop version first; treat the int-index rewrite as a clarity option afterward.
Small habits that keep reverse right-aligned pyramids clean.
Use two spaces when letters use String.fromCharCode(j).padStart(2, " ").
Always begin the slice at the top letter, not at i.
Require a single A–Z character; normalize case if needed.
Proportional fonts make width-2 cells look misaligned.
If letters run A, B C, D E F… you wrote the sequential stream instead.
Pro Tip: if the first row is A instead of E, check that the outer loop starts at the top letter and the letter loop also starts there.
Mistakes that commonly break reverse right-aligned pyramids.
Rows become single letters or wrong slices.
→ Letter loop must start at top (or E), not at i.
Empty cells become narrower than String.fromCharCode(j).padStart(2, " ") letter fields.
→ Append " " (two spaces) for each pad cell.
Columns look broken even when the code is correct.
→ View output in a monospace terminal/font.
charCodeAt on bad inputEmpty lines or multi-character input break letter logic or use only the first char.
→ Validate a single A–Z letter after strip().upper().
Using k++ produces A, B C, D E F… instead of reverse slices.
→ Print j from top down to i each row.
Check these inputs before calling the solution done.
Output is just A (no pads).
Five rows through E D C B A.
Three rows (Example 2).
Normalize with .upper() if needed.
Validate before charCodeAt.
Reject non A–Z tops so loops do not misbehave.
Try these variations to lock in the pattern.
i..top instead of top..i" " ↔ String.fromCharCode(j).padStart(2, " ")).top - 'A' + 1 (5 for E).k++ stream across rows.Quick Takeaway: pad empty width-2 cells for A..(i-1), append reverse letters top..i with matching width, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Char pad + letters (Examples 1–2) | O(n²) | O(1) |
| Int row index (Example 3) | O(n²) | O(1) |
Each of n rows does O(n) pad + letter work, so total work is O(n²).
The right-aligned reverse alphabet pyramid is a small nested-loop exercise with lasting payoff: reverse letter ranges, shrinking pads, and fixed-width cells. Master the classic E…A sample, then try user input and the int-index rewrite.
Practice the three examples above, then continue to Program 24’s palindromic alphabet pyramid.
Pad for A..(i-1), append top..i with width 2, match pad and letter widths, then break the line.
i instead of topk++ streamconsole.log(line) inside the pad or letter loopPrint the right-aligned reverse alphabet pyramid the beginner-friendly way.
Pad + reverse slice
Definitiontop..i each row
Code" " & {0,2}
CodeEnds each row
I/OO(n²) time
AnalysisOuter i runs from E down to A. The first inner loop prints one " " per j with A <= j < i (so the block shifts left each row). The second loop prints letters from E down to i using String.fromCharCode(j).padStart(2, " ") so each cell is 2 columns wide.
Next up: palindromic alphabet pyramids (A, BAB, CBABC, …).
12 people found this page helpful