A reverse-order alphabet triangle prints a left-aligned staircase where each row’s highest letter grows forward, but letters along the row print backward down to A.
Remember
Rule: on row with peak i, print i down to A
A
BA
CBA
DCBA
EDCBA ← 5 rows
It is the row-order flip of Program 1: same growing widths, but the inner loop counts down instead of up. Compare with Program 3, which changes the start letter while still printing forward.
Approach
How to Solve It
Two ways to emit the same shape — start with nested charCodeAt loops, then optionally reverse a slice.
Method
Idea
Best for
Nested charCode loops
Outer = peak letter; inner = peak down to A
Learning, interviews, exams
Reverse slice
Slice A..peak, reverse, print once
Shorter demos once loops click
Pseudocode
Pseudocode
start = code of 'A'
for i from 1 to rows:
line = ""
for code from start + i - 1 down to start:
append fromCharCode(code) to line
print line
Cheat sheet
Goal
Pattern
Walk each peak
for (let i = 1; i <= rows; i++)
Start code
const start = "A".charCodeAt(0);
Print peak down to A
for (let code = start + i - 1; code >= start; 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-order 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
A
BA
CBA
DCBA
EDCBA
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop peak and count how many times the inner loop appends.
Peak
Inner codes
Printed row
Letters
A
A..A
A
1
B
B..A
BA
2
C
C..A
CBA
3
D
D..A
DCBA
4
Total letter prints: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).
Code
JavaScript Programs
Three complete programs: fixed rows, 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 rows = 5
Hard-coded height — outer grows peaks A..E; inner counts down to A.
JavaScript
let rows = 5;
const start = "A".charCodeAt(0);
for (let i = 1; i <= rows; i++) {
let line = "";
for (let code = start + i - 1; code >= start; code--) {
line += String.fromCharCode(code);
}
console.log(line);
}
1. Outer loop picks the peak.i runs from 1 to 5 — peak letter is fromCharCode(start + i - 1).
2. Inner loop counts down.code runs from that peak down to start, so the row is peak…A.
3. Print letters, then break the line.line += stays on the row; console.log(line) after the inner loop starts the next row.
When i = 1 you get A; when i = 2 you get BA; up through EDCBA.
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 start = "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;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let code = start + i - 1; code >= start; code--) {
line += String.fromCharCode(code);
}
console.log(line);
}
}
1. Slice the forward prefix.letters.slice(0, i) is A, AB, ABC, … — same as Program 1.
2. Reverse and join. Spread into an array, reverse(), then join("") turns that prefix into A, BA, CBA, …
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++
Forward triangle by mistake
If the inner loop increments from A upward, you reprint Program 1. Keep code-- from the peak down to start.
Stop early
Missing final A
Stopping with code > start drops the tip letter every row. The condition must be code >= 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
start + rows - 1 leaves A–Z when rows > 26. Clamp or reject in interactive programs.
rows = 1
Single A
Output is just A — 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) per temporary row array/string
Total letters = 1 + 2 + … + n = n(n + 1)/2 — still quadratic in n. Same totals as Program 1; only print order along each row differs.
Remember
Key Takeaways
Rule: peak letter grows A..last; print that peak down to A.
Flip of Program 1: same widths — reverse the inner loop direction.
Break the row: call console.log only after the inner loop.
Complexity:O(n²) time from the triangular letter count.
One line: for each peak from A to the last letter, append that peak down to A, then console.log.
Frequently Asked Questions
The outer loop selects the highest letter on the row (A to E). The inner loop starts at that letter’s code and decrements down to A, appending each character, so each row reads backward.
Because the inner loop always runs until code equals the code of A, so the last printed character on every row is A.
Program 1 prints forward along each row (A, AB, ABC). This pattern prints backward down to A (A, BA, CBA) while the left edge still grows A, B, C, …
Program 3 starts earlier each row but still prints forward to a fixed top (E, DE, CDE). This pattern starts later each row and prints backward to a fixed A.
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 the total printed letters are 1+2+…+n = n(n+1)/2.
Yes. Take letters.slice(0, i), reverse the characters, and console.log the result. Nested charCode loops are better for learning; reverse is a handy shortcut later.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so letter codes stay within A–Z.
🤔
Did you know?
Each row starts one letter later (A, B, C, …), but prints backward down to A. For 5 rows, the output is A, BA, CBA, DCBA, EDCBA — the reverse of Program 1’s row order.