A right-aligned sequential alphabet pyramid prints a continuous letter stream (A, then B C, then D E F…) with empty width-2 cells on the left so columns line up.
Remember
Rule: keep one counter; pad empty cells; never reset across rows
A
B C
D E F
G H I J
K L M N O ← 5 rows (letters A–O)
Contrast Program 13 (same running counter, left-aligned, no fixed-width pads). Here each cell is 2 columns: " " for pads and ch.padStart(2, " ") for letters.
Approach
How to Solve It
Keep a counter outside the row loop. Either scan all n cells with a pad-or-letter test, or append pads then letters separately.
Method
Idea
Best for
Fixed-width scan
Walk columns; pad if empty, else next letter via k++
Learning the classic column rule
Pad + letters
Append n - i empty cells, then i sequential letters
Clearer separation of concerns
Pseudocode
Pseudocode
n = rows (e.g. 5)
k = code of 'A'
for i from 1 to n:
line = ""
for s from 1 to n - i: // empty cells
append " "
for L from 1 to i: // sequential letters
append fromCharCode(k).padStart(2, " ")
k = k + 1
print line
Cheat sheet
Goal
Pattern
Start counter
let k = "A".charCodeAt(0); — outside the outer loop
Walk each row
for (let i = 1; i <= n; i++)
Scan columns
for (let j = n; j >= 1; j--)
Empty cell
line += " ";
Letter cell
line += String.fromCharCode(k++).padStart(2, " ");
Letter budget
n(n+1)/2 <= 26 → cap n at 6 for A–Z only
End the row
console.log(line);
Printing Letters vs Starting a New Line
API
Effect
Use for
line += " " / padStart
Stays on the same row
Each empty cell and each letter cell
console.log(line)
Ends the current row
After the full-width scan / pad + letters
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count and the sequential pyramid updates instantly — including letter total and last letter.
Whole numbers from 1 to 6 (keeps letters in A–Z). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 letters · last O
A
B C
D E F
G H I J
K L M N O
Trace
Worked Walkthrough — n = 4
Trace pads, letters, and the running counter range. Letter total = 4×5/2 = 10 (A–J).
i
Pads
Letters
Printed row
1
3
A
A
2
2
B C
B C
3
1
D E F
D E F
4
0
G H I J
G H I J
Each of n rows scans n cells → O(n²) total work. The triangular letter count is also O(n²).
Code
JavaScript Programs
Three complete programs: fixed 5 rows, row-count prompt, and an explicit pad + letters style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed 5 Rows
A single counter k increments only when a letter is appended, and padStart(2, " ") keeps columns aligned.
JavaScript
let k = "A".charCodeAt(0);
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 5; j >= 1; j--) {
if (j > i) {
line += " ";
} else {
line += String.fromCharCode(k++).padStart(2, " ");
}
}
console.log(line);
}
1. Keep one counter.k starts at A and lives outside the outer loop so letters never restart.
2. Scan fixed width. Inner j walks 5 down to 1 — five cells every row.
3. Pad or letter. While j > i, append " "; otherwise append the next letter with padStart(2, " ") and bump k.
When i = 3: two pad cells, then D, E, F. The next row continues at G.
Example 2 — Row Count Input
Validate parseInt(prompt()) and cap at 6 so n(n+1)/2 <= 26 (A–Z only).
JavaScript
let n = parseInt(prompt("Enter number of rows (like 5):"), 10);
if (!Number.isFinite(n) || n < 1) {
console.log("Please enter a whole number >= 1.");
} else {
if (n > 6) n = 6; // keep letters within A–Z
let k = "A".charCodeAt(0);
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = n; j >= 1; j--) {
if (j > i) {
line += " ";
} else {
line += String.fromCharCode(k++).padStart(2, " ");
}
}
console.log(line);
}
}
1. Prompt and validate. Parse with parseInt, reject invalid values, cap at 6 for A–Z.
2. Same pad/letter core. Only the shared width follows n — the counter and padStart rules match Example 1.
Example 3 — Pad Cells, Then Letters
Often clearer: append n - i empty cells, then i sequential letters.
JavaScript
const n = 5;
let k = "A".charCodeAt(0);
for (let i = 1; i <= n; i++) {
let line = "";
for (let s = 0; s < n - i; s++) {
line += " ";
}
for (let L = 0; L < i; L++) {
line += String.fromCharCode(k++).padStart(2, " ");
}
console.log(line);
}
1. Pad first. Row i needs n - i empty width-2 cells on the left.
2. Then letters. Append i letters from the continuous counter with matching padStart(2, " ").
Same visual pyramid as the single-scan version — only the loop structure changes.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset counter
Restarting at A each row
If you reset k inside the outer loop, every row starts at A again. Keep the counter outside.
width mismatch
One space vs two
Empty cells must be " " to match ch.padStart(2, " "). A single space breaks column alignment.
past Z
Letter budget
Total letters = n(n+1)/2. Cap n at 6 (21 letters) for A–Z only, or define wrap/stop past Z.
vs Program 13
Left vs right
Program 13 is the left-aligned sequential twin without fixed-width pads. This pattern adds empty cells for right alignment.
n = 1
Single A
Output is just a width-2 A cell (no pads) — a good sanity check.
log early
Broken columns
Call console.log only after pads and letters finish for the row.
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 + letters (Example 3)
O(n²)
O(n) for the current line string
Each of n rows touches n cells (pads + letters). Letter count n(n+1)/2 is also quadratic.
Remember
Key Takeaways
Rule: one running counter; pad empty cells; never reset across rows.
Width: empty " " must match ch.padStart(2, " ").
Budget: letters = n(n+1)/2 — cap at 6 rows for A–Z.
Complexity:O(n²) from n rows × n cells.
One line: for each row i, print n-i empty cells, then i letters from a shared counter.
Frequently Asked Questions
Because the counter is not reset inside the outer loop. Each time a letter is printed we increment it so the sequence continues A, then B C, then D E F, and so on.
Letters are formatted with width 2 using ch.padStart(2, ' '). Two spaces keep empty cells the same width so columns align.
Decide a rule: stop at Z, wrap back to A, or switch to a longer alphabet list. Then apply it when incrementing the counter.
line += ' ' or line += ch.padStart(2, ' ') stays on the same row for each cell. console.log(line) ends the row after the fixed-width scan finishes.
Program 13 also uses a running letter counter, but prints left-aligned without fixed-width padding. This pattern right-aligns by printing empty 2-column cells first.
O(n²) for n rows when each row scans n slots in the inner loop.
Use parseInt(prompt(...), 10), check Number.isFinite(n), require n >= 1, and cap so n(n+1)/2 <= 26 if you want only A–Z letters (n ≤ 6).
Yes. Append n - i empty width-2 cells, then i letters via the counter. Example 3 on this page shows that style.
🤔
Did you know?
Each slot is 2 columns wide. Padding uses " " and letters use ch.padStart(2, " ") so columns line up in monospace output. The counter never resets, so letters run continuously from A to O for 5 rows.