A sequential alphabet triangle grows by one letter each row, but the alphabet does not restart — one running character continues across the whole shape.
Remember
Rule: row i has i letters; code advances after every letter
A
B C
D E F
G H I J
K L M N O ← 5 rows (15 letters, A…O)
Contrast Program 1, where each row resets to A. Here, start code at A, append and code++ inside the inner loop, then console.log(line) after the row.
Approach
How to Solve It
Keep one shared code outside the outer loop. Optionally put spaces between letters for readability.
code = code of 'A'
for i from 1 to rows:
line = ""
for j from 1 to i:
append fromCharCode(code) to line
if j < i: append space to line // optional
code = code + 1
print line
Cheat sheet
Goal
Pattern
Start once
let code = "A".charCodeAt(0); (outside the outer loop)
Append letters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the sequential triangle updates instantly — including the last letter and triangular total.
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 · last O · 15 letters
A
B C
D E F
G H I J
K L M N O
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and the letters taken from the running code.
i
Letters used
Printed row
Count
1
A
A
1
2
B, C
B C
2
3
D, E, F
D E F
3
4
G, H, I, J
G H I J
4
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 with spaces, prompt input, and a compact no-space variant. 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 with spaces between letters — ideal for first demos and screenshots.
JavaScript
let rows = 5;
let code = "A".charCodeAt(0);
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += String.fromCharCode(code);
if (j < i) {
line += " ";
}
code++;
}
console.log(line);
}
1. Start the running code once.code begins at A and is not reset each row.
2. Outer loop picks the row width. Row i needs exactly i letters.
3. Inner loop appends and advances. Append fromCharCode(code), optionally a space if more letters remain, then code++.
4. Break the line.console.log(line) after the inner loop prints the row and starts the next one.
Five rows use 15 letters: A through O.
Example 2 — User Input Version
Read the row count at runtime with prompt. Validate with parseInt and clamp to 6 so the triangular letter count stays in A–Z.
JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
let code = "A".charCodeAt(0);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number of rows >= 1.");
} else {
if (rows > 6) rows = 6; // n(n+1)/2 <= 26 for A-Z
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += String.fromCharCode(code);
if (j < i) {
line += " ";
}
code++;
}
console.log(line);
}
}
1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).
2. Validate and clamp. Reject NaN or non-positive values; cap at 6 so n(n+1)/2 ≤ 21 stays in A–Z.
3. Same running-code core. Only the source of rows changes — the print logic matches Example 1.
Example 3 — No Spaces
Same continuous sequence without spaces between letters — denser console output.
JavaScript
let rows = 5;
let code = "A".charCodeAt(0);
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += String.fromCharCode(code);
code++;
}
console.log(line);
}
1. Same running code. Still start once at A and advance after every letter.
2. Skip the space. Only line += fromCharCode(code) and code++ remain in the inner loop.
3. Print and advance.console.log still ends each row after the inner loop.
Learn the spaced version first (Examples 1–2) for readability; treat this as a compact variant afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset code
Looks like Program 1
If you set code = "A".charCodeAt(0) inside the outer loop, every row restarts at A — that is Program 1, not this pattern.
code++ outside
Repeated letters on a row
Increment inside the inner loop. Advancing only once per row repeats the same letter across the width.
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.
past Z
Beyond the alphabet
For A–Z only, keep n(n+1)/2 ≤ 26 (max 6 rows) or stop when code > "Z".charCodeAt(0).
rows = 1
Single A
Output is just A on one line — 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
Spaced (Examples 1–2)
O(n²)
O(n) for the current line string
Compact (Example 3)
O(n²)
O(n) for the current line string
Total letters logged = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: row i has i letters; the alphabet continues across rows.
Running code: declare once; code++ inside the inner loop.
Break the row: call console.log(line) only after the inner loop.
Complexity:O(n²) time from the triangular letter count.
One line: start at A, for each row append the next letters with code++, then console.log.
Frequently Asked Questions
Because the letter code is updated after every appended character and is not reset inside the outer loop. That is what makes the sequence continuous across the triangle.
A single running character starts at A and increments after every append. Row 1 logs 1 character, row 2 logs 2, row 3 logs 3, so you see consecutive letters across the whole triangle.
Because each cell must use the next letter. If you incremented only once per row, wider rows would repeat the same letter.
line += String.fromCharCode(code) (and optional spaces) builds the full row string. console.log() inside the inner loop would log one letter per line. Log once 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 logged letters equal n(n+1)/2.
Plain code++ continues past Z into later Unicode code points. Cap rows so n(n+1)/2 ≤ 26 (max 6 rows for A–Z only), or stop when code > "Z".charCodeAt(0).
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp so the triangular letter count stays in range for your alphabet policy.
🤔
Did you know?
Unlike Program 1 (letters reset to A each row), this pattern uses one running character that increments after every append. Letters stay consecutive across the whole triangle: A, then B C, then D E F, and so on.