An inverted forward repeating alphabet triangle prints one letter per row with a shrinking width, while letters advance from A toward the top letter.
Remember
Rule: widths shrink n…1; letter steps A → top
AAAAA
BBBB
CCC
DD
E ← 5 rows (top = E)
Same shrinking widths as Program 11, but letters run A → B → C → D → E instead of counting down. Outer loop picks the letter; inner loop appends it top - code + 1 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 A…top; inner repeats that letter fewer times each row
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 base to top:
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 forward
for (let code = base; code <= top; code++)
Shrink 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 inverted forward triangle updates instantly — including the last 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 · last E · 15 letters
AAAAA
BBBB
CCC
DD
E
Trace
Worked Walkthrough — rows = 4
With rows = 4, top letter is D. Trace each outer-loop code and the shrinking repeat count.
code
Letter
repeat
Printed row
A
A
4
AAAA
B
B
3
BBB
C
C
2
CC
D
D
1
D
Total letter characters: 4 + 3 + 2 + 1 = 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; first row is five As.
JavaScript
let rows = 5;
const base = "A".charCodeAt(0);
const top = base + rows - 1;
for (let code = base; code <= top; 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 advances letters.code runs from base up to top. Each value is the letter for that row.
3. Inner loop shrinks the width.repeat = top - code + 1 gives widths n, n-1, …, 1. 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 AAAAA; next is BBBB; last is a single E.
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 = base; code <= top; 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 advance. Still walk code from base up to top.
2. Build the row.ch.repeat(repeat) returns the letter repeated repeat times (n down to 1).
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.
wrong direction
Countdown instead of forward
Walk code from base to top. Counting down from top is Program 11.
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.
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 = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: widths shrink n…1; letters advance from A to the top letter.
Two loops: outer = letter A…top; 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 A to top, append it top - code + 1 times, then console.log.
Frequently Asked Questions
Program 11 prints EEEEE, DDDD, … (letters step down). Program 12 prints AAAAA, BBBB, … (letters step up) with the same inverted widths 5…1.
When code is A, repeat = top − A + 1 = 5, so five A’s. Next code is B, repeat = 4, so four B’s.
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.
repeat = top − code + 1. As code rises toward top, fewer iterations run, so fewer characters are appended.
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 n+(n−1)+…+1 = 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 letters stay within A–Z.
🤔
Did you know?
This is the forward-letter twin of Program 11: same inverted widths (5…1), but letters advance A→E instead of stepping down. Append the outer loop letter inside the inner loop so each row stays uniform.