Shape Rule
Growing prefixes
A, A B, A B C, … A B C D E.

Right-align the pyramid by printing a shrinking number of leading spaces, then printing letters from A up to the current row letter (restarting each row). Because we append letters using String.fromCharCode(k).padStart(2, " "), use a monospace font if you want the right edge to look perfect. Compare Program 22 (right-aligned sequential stream) and Program 16 (centered). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Growing prefixes
A, A B, A B C, … A B C D E.
Shrink spaces
Print top - i spaces so rows share a right edge.
Each row
Letters always run A..i — not a k++ stream.
{0,2}
Fixed-width letter cells for even columns.
Top letter
Pick a top letter (A–F) and draw the pyramid.
Complexity
Pads + letters per row scale with n.
A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed to the right with leading spaces so every row shares the same right edge.
In JavaScript you solve it with nested loops over letter codes: shrink the pad count, then append A..i with String.fromCharCode(k).padStart(2, " ") formatting.
It combines padding math with per-row letter prefixes — the classic right-aligned triangle before sequential streams or centering.
Shrink leading spaces per row.
Restart letters every row.
All rows share the same end.
Not Program 22’s continuous k++.
In short: for each row letter i, append spaces while j > i, then append A..i with padStart(2, " "), then call console.log(line).
Given a top letter (or fixed E), print a right-aligned pyramid of alphabet prefixes ending at that letter.
// Five rows (monospace; leading spaces + width-2 letters)
// A
// A B
// A B C
// A B C D
// A B C D E | Item | Type | Description |
|---|---|---|
top | char | Last row letter (e.g. E). Row count = top - 'A' + 1. |
| Printed output | text | Right-aligned prefixes A..i with leading spaces. |
for i from base to top:
line = ""
for j from top down while j > i:
line += " "
for k from base to i:
line += String.fromCharCode(k).padStart(2, " ")
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Char loops (classic) | Pad top..i+1; letters A..i | Matching this sample |
| Int row index | Pad n-row; letters by index | When you prefer int counters |
| Goal | Pattern |
|---|---|
| Outer rows | for (let i = base; i <= top; i++) |
| Leading pads | for (let j = top; j > i; j--) line += " " |
| Letters | for (let k = base; k <= i; k++) line += String.fromCharCode(k).padStart(2, " ") |
| End row | console.log(line) |
| Sequential stream | See Program 22 |
Same row - three roles that build the right-aligned pyramid.
padLeading spaces that shrink each row
A..iPrefix letters restarting from A
growEach row adds one more letter on the right
breakEnds the row after pads + letters
Reach for this when teaching leading-space alignment with per-row alphabet prefixes.
First right-aligned alphabet pyramid many courses assign.
Same right edge idea; prefixes vs continuous stream.
Step up to Program 16 after pads feel natural.
Practice String.fromCharCode(k).padStart(2, " ") letter cells in monospace output.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: shrinking leading spaces while restarting A..i is the clearest way to teach right-aligned alphabet prefixes.
Choose a top letter from A to F and draw the right-aligned alphabet pyramid in the browser (monospace).
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 prefix rows from A through E.
First append leading spaces, then append letters A..i using padStart(2, " ").
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);
} When i is 'C', two leading spaces append, then letters A B C via padStart(2, " "). The next row pads once and prints through D, keeping the right edge fixed.
Let the user choose the last letter (like E).
The pattern prints up to that row. Use prompt().trim().toUpperCase() and validate a single A–Z character in real apps.
let topCh = prompt("Enter top letter (like E):");
topCh = (topCh || "").trim().toUpperCase();
if (topCh.length !== 1 || !/^[A-Z]$/.test(topCh)) {
console.log("Please enter a single letter.");
} else {
const base = "A".charCodeAt(0);
const top = topCh.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);
}
} Same pad + prefix rules; only the shared top letter changes. Pad count is always top - i spaces.
Same shape with integer row and column indexes.
Often clearer if you think in row numbers: pad n - row spaces, then print row letters from A.
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 L = 0; L < row; L++) {
line += String.fromCharCode(base + L).padStart(2, " ");
}
console.log(line);
} Row 1 prints one letter; row 5 prints five. Pad count is n - row; letter L is String.fromCharCode(base + L).
Outer i runs from A to E (or your chosen top).
Loop j = E..(i+1) prints one space per step, making the pyramid right-aligned.
Loop k = A..i appends each letter in a 2-character field using String.fromCharCode(k).padStart(2, " ").
console.log(line) ends the row so the next lower pad count can grow the prefix.
Each row does O(n) work for padding plus letters, so total is O(n²).
Trace each row’s pad count, letter prefix, and printed line.
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.
Where this right-aligned prefix pyramid shows up beyond the homework prompt.
Clearest demo of shrinking leading spaces for right alignment.
Example: remove pads once and see a left-aligned triangle.
Same right edge — prefixes vs continuous letter stream.
Example: print both for top = E side by side.
Outer and inner loops over ascending char ranges.
Example: rewrite with int indexes (Example 3).
Use String.fromCharCode(k).padStart(2, " ") so letter columns stay even.
Example: try plain Write(k) and compare spacing.
After right-align, add more pads for a centered look.
Example: see Program 16.
Next pattern builds a symmetric decreasing alphabet square.
Example: continue to Program 28.
Pro Tip: say “fewer spaces, then A through the row letter” before coding - that story prevents a continuous k++ stream by mistake.
Why this pattern earns a spot early in the alphabet-pattern series.
Wrong pad counts or continuous streams show up immediately.
Char loops or int indexes teach the same shape.
A natural place to learn leading-space alignment.
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 right-aligned prefix pyramids clean.
Always append A..i — do not keep a running k++ for this pattern.
Pad count is top - i; last row has zero pads.
Require a single A–Z character; normalize case if needed.
Proportional fonts make String.fromCharCode(k).padStart(2, " ") columns look uneven.
Mixing one-space and two-space pads breaks the right edge.
Pro Tip: if rows look like A, B C, D E F, you wrote Program 22’s stream instead of restarting at A.
Mistakes that commonly break right-aligned alphabet prefix pyramids.
Rows become A, B C, D E F… instead of A, A B, A B C.
→ Restart letters from A on every row.
Using j >= i or the wrong bound leaves uneven right edges.
→ Pad while j > i from top downward.
Columns look uneven even when the code is correct.
→ View output in a monospace terminal/font.
Empty lines or multi-character input break letter logic or use only the first char.
→ Validate a single A–Z letter after trim().toUpperCase().
Switching between one- and two-space pads breaks the right edge.
→ Keep pad characters consistent for the whole program.
Check these inputs before calling the solution done.
Output is just A (no pads).
Five rows through A B C D E.
Three rows (Example 2).
Normalize with .upper() if needed.
Validate before calling charCodeAt(0).
Skip the pad loop for a left triangle.
Try these variations to lock in the pattern.
k++ instead of A..iA and grows through the row letter.String.fromCharCode(k).padStart(2, " ") keeps letter columns even in monospace terminals.Quick Takeaway: print shrinking leading spaces, then letters A..i with width 2, 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 alphabet pyramid is a small nested-loop exercise with lasting payoff: shrinking leading spaces and per-row prefixes from A. Master the classic A…E sample, then try user input and the int-index rewrite.
Practice the three examples above, then continue to Program 28’s symmetric decreasing alphabet square.
Pad while j > i, print A..i with width 2, keep pads consistent, then break the line.
A on every rowString.fromCharCode(k).padStart(2, " ") output in a monospace fontk++ stream for this patternconsole.log(line) inside the pad or letter loopPrint the right-aligned alphabet pyramid the beginner-friendly way.
Pad + A..i
DefinitionRestart each row
Code{0,2} cells
CodeEnds each row
I/OO(n²) time
AnalysisLeading padding: for each row letter i, the loop appends top - i spaces. Then the letter loop appends A through i using String.fromCharCode(k).padStart(2, " ") so columns look even in monospace output. The last row has no padding; all rows share the same right edge.
Next up: symmetric decreasing alphabet squares (E…A…E layers).
12 people found this page helpful