A reverse alphabet decreasing triangle prints each row backward down to A, while the starting letter moves one step earlier each row — so the left edge slides inward.
Remember
Rule: for start letter i from last down to A,
print i down through A
EDCBA
DCBA
CBA
BA
A ← 5 rows (every row ends at A)
Same widths as Program 6 (5, 4, 3, 2, 1), but Program 6 prints forward to a fixed end (ABCDE, BCDE, …). Here both loops decrement. Compare with Program 8, which keeps E fixed on the left and shortens the tail.
Approach
How to Solve It
Two ways to emit the same shape — start with nested charCodeAt loops, then optionally reverse a prefix once and slice.
Method
Idea
Best for
Nested charCode loops
Outer = lowering start; inner = start..A downward
Learning, interviews, exams
Reverse + slice(i)
Build EDCBA… once, drop one left letter each row
Shorter demos once loops click
Pseudocode
Pseudocode
base = code of 'A'
top = base + rows - 1
for start from top down to base:
line = ""
for code from start down to base:
append fromCharCode(code) to line
print line
Cheat sheet
Goal
Pattern
Lower start code
for (let start = top; start >= base; start--)
Print start..A reverse
for (let code = start; code >= base; code--) line += String.fromCharCode(code);
Append letters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the reverse decreasing triangle updates instantly — capped at 26 letters (A–Z).
Whole numbers from 1 to 26. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 letters
EDCBA
DCBA
CBA
BA
A
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop start as start counts down from D to A.
Start
Inner codes
Printed row
Letters
D
D..A
DCBA
4
C
C..A
CBA
3
B
B..A
BA
2
A
A..A
A
1
Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same triangular count as Programs 1 and 6.
Code
JavaScript Programs
Three complete programs: fixed top letter, prompt input, and a reverse-slice shortcut. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed from E down to A
Hard-coded top — both loops decrement; every row ends at A.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let start = top; start >= base; start--) {
let line = "";
for (let code = start; code >= base; code--) {
line += String.fromCharCode(code);
}
console.log(line);
}
1. Outer loop picks the start letter.start runs from E down to A — longest row first.
2. Inner loop walks down to A. For each start, code runs from start down to base, so the row is start…A in reverse.
3. Print letters, then break the line.line += stays on the row; console.log(line) after the inner loop starts the next (shorter) row.
When start is E you get EDCBA; when it is A you get A.
Example 2 — User Input Version
Read the row count at runtime with prompt. Validate with parseInt and clamp to 26 for A–Z demos.
JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
const base = "A".charCodeAt(0);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number of rows >= 1.");
} else {
if (rows > 26) rows = 26;
const top = base + rows - 1;
for (let start = top; start >= base; start--) {
let line = "";
for (let code = start; code >= base; code--) {
line += String.fromCharCode(code);
}
console.log(line);
}
}
1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).
2. Map rows to a top code.top = base + rows - 1 — for rows = 4, top is the code of D.
3. Same reverse-shrink core. Only the source of top changes — the print logic matches Example 1.
Example 3 — Reverse + slice(i)
Build the first reverse row once, then drop one left letter each time — same shape, no nested letter loop.
JavaScript
let rows = 5;
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const top = [...letters.slice(0, rows)].reverse().join("");
for (let i = 0; i < rows; i++) {
console.log(top.slice(i));
}
1. Take and reverse the prefix.ABCDE reversed becomes EDCBA — the first printed row.
2. Slice from index i.slice(0) is the full row; slice(1) drops E; and so on down to A.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
code++
Program 6 by mistake
If the inner loop increments instead of decrements, you print forward suffixes. Use for (let code = start; code >= base; code--).
Fixed start
Program 8 shape
If the outer loop stays on E and only shortens the stop, you get EDCBA, EDCB, … Lower the start with start--.
log inside
Column of letters
If console.log is inside the inner loop, each letter lands on its own line. Append with +=; call console.log only after the inner loop.
rows > 26
Past Z
Codes leave A–Z when rows > 26. Clamp or reject in interactive programs.
rows = 1
Single A
Output is just A — start and end coincide. A good sanity check.
Bad prompt
Use Number.isFinite
Bare parseInt(prompt()) yields NaN on letters — validate before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(n) for the current line string
Reverse + slice (Example 3)
O(rows²)
O(n) for the reversed prefix and temporary row strings
Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Same totals as Programs 1 and 6.
Remember
Key Takeaways
Rule: lower start letter; print that start down through A each row.
vs Program 6: same widths — here letters run backward to A; there they run forward to a fixed end.
Break the row: call console.log only after the inner loop.
Complexity:O(n²) time from the triangular letter count.
One line: for start from the top letter down to A, append that start down through A, then console.log.
Frequently Asked Questions
The outer loop picks the starting letter (E, then D, then C…). The inner loop prints from that start down to A. Each row drops the previous leftmost letter and becomes shorter.
The outer loop decreases the starting letter each row, and the inner loop decreases letters within that row down to A. That matches the reverse-alphabet output.
Because the inner loop condition includes code == start of A, so the final character printed on each row is always A.
Program 6 prints forward suffixes ending at E (ABCDE, BCDE, …). This pattern prints reverse runs ending at A (EDCBA, DCBA, …). Same shrinking widths; opposite letter direction.
Program 8 always starts each row at E and shortens the tail (EDCBA, EDCB, EDC). Program 7 changes the first letter each row (E, then D, then C…).
line += stays on the same line. console.log ends the current line. Letters use +=; the row break uses console.log after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so letter codes stay within A–Z.
🤔
Did you know?
Both loops run backward: the outer loop lowers the start letter; the inner loop prints down to A. For 5 rows: EDCBA, DCBA, CBA, BA, A. Same widths as Program 6, opposite letter direction.