A continuous decreasing alphabet triangle prints letters in one unbroken sequence while row length shrinks: longest row first (A B C D E), then F G H I, … down to a single letter.
Remember
Rule: one running counter; row lengths n, n-1, …, 1
A B C D E
F G H I
J K L
M N
O ← 5 rows (letters A–O)
Contrast Program 13 (same counter, growing rows) and Program 5 (decreasing rows but letters reset to A each line).
Approach
How to Solve It
Keep one counter outside the outer loop. Walk row length from n down to 1, appending that many letters each time.
Method
Idea
Best for
String append
line += ch + " " then console.log(line)
Learning the classic style
row.join(" ")
Push letters into an array, join once
No trailing space
Pseudocode
Pseudocode
rows = n (e.g. 5)
code = code of 'A'
for rowLen from rows down to 1:
line = ""
repeat rowLen times:
append fromCharCode(code) (+ space)
code = code + 1
print line
Cheat sheet
Goal
Pattern
Start counter
let code = "A".charCodeAt(0); — outside the outer loop
Shrink row length
for (let rowLen = rows; rowLen >= 1; rowLen--)
Append letters
for (let i = 0; i < rowLen; i++) { line += String.fromCharCode(code) + " "; code++; }
Clean join
row.push(ch); then console.log(row.join(" "));
Letter budget
n(n+1)/2 <= 26 → cap n at 6 for A–Z only
End the row
console.log(line);
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch + " "
Stays on the same row
Each letter (and its separator)
console.log(line)
Ends the current row
After the inner loop
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count and the decreasing continuous triangle updates instantly — including letter total and last letter.
Whole numbers from 1 to 6 (keeps letters in A–Z). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 letters · last O
A B C D E
F G H I
J K L
M N
O
Trace
Worked Walkthrough — rows = 4
Trace each row length and the continuous letter range. Total = 4×5/2 = 10 (A–J).
rowLen
Letters
Printed row
4
A..D
A B C D
3
E..G
E F G
2
H..I
H I
1
J
J
Total letters n(n+1)/2 → O(n²) work.
Code
JavaScript Programs
Three complete programs: fixed 5 rows, row-count prompt, and a join style with no trailing space. 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.
JavaScript
const rows = 5;
let code = "A".charCodeAt(0);
for (let rowLen = rows; rowLen >= 1; rowLen--) {
let line = "";
for (let i = 0; i < rowLen; i++) {
line += String.fromCharCode(code) + " ";
code++;
}
console.log(line);
}
1. Keep one counter.code starts at A and lives outside the outer loop so letters never restart.
2. Shrink row length.rowLen walks from 5 down to 1 — longest row first.
3. Append and bump. Each inner iteration appends the current letter and does code++.
First row takes A–E; the next continues at F — continuous across the whole triangle.
Example 2 — User Input Version
Read rows and clamp to 1–6 for A–Z demos (6×7/2 = 21 letters).
JavaScript
let rows = parseInt(prompt("Enter number of rows (max 6):"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number >= 1.");
} else {
if (rows > 6) rows = 6;
let code = "A".charCodeAt(0);
for (let rowLen = rows; rowLen >= 1; rowLen--) {
let line = "";
for (let i = 0; i < rowLen; i++) {
line += String.fromCharCode(code) + " ";
code++;
}
console.log(line);
}
}
1. Same code++ logic. Only formatting changes — letters still continue across rows.
2. Join once.row.join(" ") inserts spaces between letters without a trailing space at the end of the line.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset counter
Restarting at A each row
If you reset code inside the outer loop, you get Program 5 (ABCDE, ABCD, …). Keep the counter outside.
vs Program 13
Growing vs shrinking
Program 13 uses 1..n (growing). This pattern uses n..1 (shrinking) with the same continuous counter.
past Z
Letter budget
Total letters = n(n+1)/2. Cap n at 6 for A–Z only, or define wrap/stop past Z.
wrong direction
1..n instead of n..1
Looping rowLen upward prints Program 13’s growing shape, not this decreasing triangle.
n = 1
Single A
Output is just A — a good sanity check.
log early
One letter per line
Call console.log only after the inner loop finishes building the full row.
Analysis
Time and Space Complexity
Program
Time
Extra space
String append (Examples 1–2)
O(n²)
O(n) for the current line string
join style (Example 3)
O(n²)
O(n) for the current row array
Total letters are n(n+1)/2, so work is quadratic in the number of rows either way.
Remember
Key Takeaways
Rule: one running counter; row lengths n, n-1, …, 1.
Never reset: keep code outside the outer loop.
Budget: letters = n(n+1)/2 — cap at 6 rows for A–Z.
Complexity:O(n²) from the triangular letter count.
One line: for each length from n down to 1, print that many letters from a shared counter.
Frequently Asked Questions
Because code is initialized once before the outer loop and incremented after every printed character. The counter never resets, so letters stay consecutive across the whole triangle.
Program 13 grows row width (1, 2, 3, … letters). This pattern shrinks width (n, n-1, …, 1) while using the same running counter — A B C D E then F G H I, not A then B C.
Program 5 also uses decreasing row lengths but resets to A each row (ABCDE, ABCD, ABC, …). Here letters continue: A B C D E, F G H I, J K L, M N, O.
That yields rows, rows-1, …, 1 — exactly how many letters each row needs, longest first. Program 13 uses 1..rows for the opposite (growing) shape.
line += String.fromCharCode(code) + ' ' stays on the same row for each letter. console.log(line) ends the current row after the inner loop finishes.
1+2+…+n = n(n+1)/2. For 5 rows that is 15 letters (A through O).
O(n²) where n is the number of rows. Total printed letters equal n(n+1)/2.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), then clamp between 1 and 6 so n(n+1)/2 stays within A–Z for demos.
🤔
Did you know?
One running counter prints letters continuously while row length shrinks: 5 letters, then 4, 3, 2, 1. Total letters for n rows is still n(n+1)/2 — compare Program 13 (growing rows) and Program 5 (decreasing rows but letters reset each line).