An inverted alphabet right-angled triangle prints the longest letter run first, then shrinks by one letter each row — still starting from A every time.
Remember
Rule: for end letter i from last down to A,
print A through i
ABCDE
ABCD
ABC
AB
A ← 5 rows (longest on top)
It is the flip of Program 1: keep the same inner A..i print, reverse only the outer loop. Compare with Program 6, which shrinks from the left instead.
Approach
How to Solve It
Two ways to emit the same shape — start with nested charCodeAt loops, then optionally shorten with slice.
Method
Idea
Best for
Nested charCode loops
Outer = shrinking end; inner = A..end
Learning, interviews, exams
letters.slice(0, i)
Slice shorter prefixes while i counts down
Shorter demos once loops click
Pseudocode
Pseudocode
start = code of 'A'
top = start + rows - 1
for end from top down to start:
line = ""
for code from start to end:
append fromCharCode(code) to line
print line
Cheat sheet
Goal
Pattern
Countdown length
for (let i = rows; i >= 1; i--)
Start code
const start = "A".charCodeAt(0);
Print A..end
for (let code = start; code < start + i; 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 inverted 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
ABCDE
ABCD
ABC
AB
A
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop length as i counts down from 4 to 1.
Length i
Codes
Printed row
Letters
4
A..D
ABCD
4
3
A..C
ABC
3
2
A..B
AB
2
1
A..A
A
1
Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same as Program 1, only the print order of rows differs.
Code
JavaScript Programs
Three complete programs: fixed rows, prompt input, and a 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 loop shrinks; inner loop still prints A through the current length.
JavaScript
let rows = 5;
const start = "A".charCodeAt(0);
for (let i = rows; i >= 1; i--) {
let line = "";
for (let code = start; code < start + i; code++) {
line += String.fromCharCode(code);
}
console.log(line);
}
1. Outer loop picks the length.i runs from 5 down to 1 — longest row first.
2. Inner loop restarts at A. For each i, code runs from start through start + i - 1, so the row is A through the i-th letter.
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 i = 5 you get ABCDE; when i = 1 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 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 = rows; i >= 1; i--) {
let line = "";
for (let code = start; code < start + i; code++) {
line += String.fromCharCode(code);
}
console.log(line);
}
}
2. Slice the prefix.letters.slice(0, i) is ABCDE, then ABCD, and so on.
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.
i++
Growing triangle by mistake
If you increment i from 1 to rows, you reprint Program 1. Use i-- from rows down to 1.
Stop at 2
Missing final A
Stopping before i = 1 drops the tip row. The condition must be i >= 1.
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 — longest and shortest 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
letters.slice(0, i) (Example 3)
O(rows²)
O(n) per temporary row string
Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as Program 1.
Remember
Key Takeaways
Rule: countdown length i; print A through the i-th letter each row.
Flip of Program 1: same inner loop — only reverse the outer loop.
Break the row: call console.log only after the inner loop.
Complexity:O(n²) time from the triangular letter count.
One line: for length i from rows down to 1, append A through the i-th letter, then console.log.
Frequently Asked Questions
The outer loop counts down so the first row prints the most letters and each next row prints one fewer. The inner loop still prints from A up to the current bound.
Because the inner loop always starts at A. That resets the sequence each row, producing ABCDE then ABCD and so on.
Program 1 grows row length (A, AB, ABC). This pattern shrinks it (ABCDE, ABCD, ABC). Only the outer loop direction changes; the inner loop is the same A..i print.
Program 4 grows while printing backward (A, BA, CBA). This pattern shrinks while printing forward from A (ABCDE, ABCD, ABC).
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.
Yes. Keep a string of A–Z and console.log(letters.slice(0, i)) while i counts down from rows to 1. Nested charCode loops are better for learning; slice 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?
This is the inverted twin of Program 1: same inner A..i print, only the outer loop shrinks. For 5 rows: ABCDE, ABCD, ABC, AB, A. Total letters still equal n(n+1)/2.