A reverse repeating alphabet triangle prints one letter per row, repeated to match the row width, while letters count down from the top letter toward A.
Remember
Rule: row width grows 1, 2, 3…; letter steps top → A
E
DD
CCC
BBBB
AAAAA ← 5 rows (top = E)
Same growing widths as Program 9, but letters run E → D → C → B → A. Solve it with nested loops: outer picks the letter (counting down), inner appends that letter the right number of times, then console.log(line) ends the row.
Approach
How to Solve It
Two ways to emit the same shape — start with nested loops, then optionally shorten with String.repeat.
Method
Idea
Best for
Nested loops
Outer = letter countdown; inner repeats that letter
Learning, interviews, exams
ch.repeat(n)
Build a full repeated-letter row in one call
Shorter demos once loops click
Pseudocode
Pseudocode
base = code of 'A'
top = base + rows - 1
for code from top down to base:
ch = fromCharCode(code)
repeat = top - code + 1
line = ""
for k from 1 to repeat:
append ch to line
print line
Cheat sheet
Goal
Pattern
Top letter for n rows
const top = "A".charCodeAt(0) + rows - 1;
Walk letters down
for (let code = top; code >= base; code--)
Repeat count
const repeat = top - code + 1;
Append outer letter
line += String.fromCharCode(code); (not the inner index)
Append letters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the reverse repeating triangle updates instantly — including the top letter and triangular total.
Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · top E · 15 letters
E
DD
CCC
BBBB
AAAAA
Trace
Worked Walkthrough — rows = 4
With rows = 4, top letter is D. Trace each outer-loop code and the repeated letter on that row.
code
Letter
repeat
Printed row
D
D
1
D
C
C
2
CC
B
B
3
BBB
A
A
4
AAAA
Total letter characters: 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 String.repeat 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 — ideal for first demos and screenshots. Top letter becomes E.
JavaScript
let rows = 5;
const base = "A".charCodeAt(0);
const top = base + rows - 1;
for (let code = top; code >= base; code--) {
const ch = String.fromCharCode(code);
const repeat = top - code + 1;
let line = "";
for (let k = 0; k < repeat; k++) {
line += ch;
}
console.log(line);
}
1. Set height and bounds.rows = 5; base is A; top is E (base + rows - 1).
2. Outer loop counts letters down.code runs from top down to base. Each value is the letter for that row.
3. Inner loop repeats that letter.repeat = top - code + 1 gives widths 1, 2, 3, … Append ch (the outer letter), not the inner index.
4. Break the line.console.log(line) after the inner loop prints the row and starts the next one.
First row is E; second is DD; last is AAAAA.
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 code = top; code >= base; code--) {
const ch = String.fromCharCode(code);
const repeat = top - code + 1;
let line = "";
for (let k = 0; k < repeat; k++) {
line += ch;
}
console.log(line);
}
}
1. Same outer countdown. Still walk code from top down to base.
2. Build the row.ch.repeat(repeat) returns the letter repeated repeat times.
3. Print and advance.console.log prints that string and ends the line.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
inner letter
Mixed letters on a row
Append the outer letter (ch), not the inner counter. Printing the inner index changes letters across the row.
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.
Reuse line
Growing leftovers
Reset line = "" at the start of each outer iteration, or letters from previous rows stick around.
rows = 1
Single A
Output is just A on one line — a good sanity check (top === base).
rows > 26
Beyond Z
Clamp or reject — top leaves A–Z. Keep rows in 1…26 for alphabet demos.
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(n²)
O(n) for the current line string
ch.repeat (Example 3)
O(n²)
O(n) per temporary row string
Total letters logged = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: widths grow 1, 2, 3…; letters step from the top letter down to A.
Two loops: outer = letter countdown; inner repeats that outer letter.
Break the row: call console.log(line) only after the inner loop.
Complexity:O(n²) time from the triangular letter count.
One line: for each letter from top down to A, append it repeat times, then console.log.
Frequently Asked Questions
Program 9 uses A, BB, CCC, … (letters increase). Program 10 uses E, DD, CCC, … (letters decrease) with the same growing repeat counts.
On the 4th row the outer-loop letter is B, and the inner loop runs four times, appending B each time.
The inner loop only controls how many times to append. Using the outer letter keeps the row uniform; using the inner counter would change letters across the row.
line += ch builds the full row string. console.log() inside the inner loop would log one letter per line. Log once after the inner loop finishes.
O(n²) where n is the number of rows. Total logged characters equal 1+2+…+n = n(n+1)/2.
Yes. console.log(ch.repeat(repeat)) logs a full repeated-letter row in one call. Nested loops are better for learning; ch.repeat is a handy shortcut later.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so the top letter stays within A–Z.
Top letter math can go past Z. Clamp to 26 for A–Z demos, or define a clear wrap/error policy.
🤔
Did you know?
This pattern is the reverse of Program 9: widths still grow 1, 2, 3, …, but letters run backward (E, then D, then C, …). Append the outer loop letter inside the inner loop so each row stays uniform.