Shape Rule
Grow width, keep sequence
Row i prints i consecutive letters from a running counter.

Each row is wider than the last, but letters stay in order across the whole shape: A, then B C, then D E F, up to K L M N O for five rows. This differs from Program 1 (letters reset per row); here one counter walks the alphabet continuously. Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Grow width, keep sequence
Row i prints i consecutive letters from a running counter.
Never reset
code = "A".charCodeAt(0) lives outside the outer loop and advances across rows.
Print then code++
Each cell prints String.fromCharCode(code), optionally a space, then code++.
Same line / next line
Letters use line += ch; end each row with console.log(line).
1–7 rows
Pick a row count and draw the sequential triangle in the browser.
Complexity
Total letters = n(n+1)/2; extra memory stays O(1).
A sequential alphabet triangle grows by one letter on each new line, but the alphabet does not restart. Letters flow continuously: the last letter on one row is followed by the next letter on the next row.
In JavaScript you usually solve it with nested for loops plus a third variable code that increments after every print. Optional spaces between letters make the output easier to read.
It is the classic “running counter” pattern. Once you can keep state across rows, many continuous fill patterns (letters, digits, or custom sequences) become straightforward.
One code walks A, B, C… across the whole triangle.
Outer loop still prints 1, 2, 3, … letters per row.
code++ belongs inside the inner loop, not after the row.
Program 1 resets to A each row; this one never resets.
In short: start code = "A".charCodeAt(0), for each row print i letters with line += String.fromCharCode(code) then code++, and call console.log(line) after the row.
Given a positive integer rows, print a left-aligned triangle of consecutive alphabet letters where row i has i letters and the sequence never resets.
// First 5 rows (with spaces)
// A
// B C
// D E F
// G H I J
// K L M N O | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines. For A–Z only, keep rows(rows+1)/2 ≤ 26 (max 6 full rows, or 7 with overflow past Z). |
| Printed output | text | Left-aligned consecutive letters; optional spaces between letters on a row. |
code = "A".charCodeAt(0)
for i from 1 to rows:
for j from 1 to i:
print String.fromCharCode(code) (no newline)
if j < i: append space
code++
console.log the row | Approach | Idea | Best for |
|---|---|---|
Running code | Outer width + inner print/code++ | Learning and interviews |
| Integer index | ch = String.fromCharCode("A".charCodeAt(0) + count); count++ | Same idea with an int counter |
| Goal | Pattern |
|---|---|
| Start the sequence | code = "A".charCodeAt(0) (outside outer loop) |
| Grow row width | for (let i = 1; i <= rows; i++) |
| Print next letter | line += String.fromCharCode(code); code++ |
| Space between letters | if (j < i) line += " " |
| End the row | console.log(line) |
| Reset-per-row style | See Program 1 (A, AB, ABC, …) |
Same triangle - different roles for each tool.
same linePrints a letter or space without moving to the next line
new lineEnds the current row after all letters are printed
next letterAdvances the running character after each cell
no resetDo not set code = "A".charCodeAt(0) inside the outer loop
Reach for a running counter when values must continue across rows.
Contrast reset-per-row letters with a continuous sequence.
Practice keeping mutable state outside the outer loop.
Same idea works with numbers or any ordered token stream.
Next: odd-length rows that still start from A each time.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one small program that locks in continuous state across nested loops - a skill used far beyond alphabet demos.
Choose a row count between 1 and 7 and draw the sequential alphabet triangle in the browser (spaces between letters).
Three complete JavaScript programs - fixed rows with spaces, prompt input, and a compact no-space variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five sequential rows with a running character and spaces.
rows = 5Hard-coded height - ideal for first demos and screenshots.
let code = "A".charCodeAt(0);
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += String.fromCharCode(code);
if (j < i) {
line += " ";
}
code++;
}
console.log(line);
} code starts at "A".charCodeAt(0) and never resets. Row 1 builds A, row 2 builds B C, and so on. code++ after each letter keeps the sequence continuous.
Let the user choose the height at runtime.
Read rows and keep appending. Validate parseInt(prompt()) with Number.isFinite in real apps; cap rows for A-Z if needed.
const rowsInput = prompt("Enter the number of rows:");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 7));
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);
} Same running-code core as Example 1; only the outer bound changes. For A-Z-only demos, stop when code > "Z".charCodeAt(0) or clamp so rows(rows+1)/2 ≤ 26.
Same sequence without spaces between letters.
Drop the space append for a denser triangle.
let code = "A".charCodeAt(0);
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += String.fromCharCode(code);
code++;
}
console.log(line);
} Same continuous code++ logic as Example 1, without spaces between letters on a row. Great for denser console output; keep the spaced version when readability matters.
Use prompt() when reading input. Start code = "A".charCodeAt(0) before the outer loop.
for (let i = 1; i <= rows; i++) decides how many letters this row prints.
Print String.fromCharCode(code), optional space, then code++ so the next cell gets the next letter.
console.log(line) ends the row; code keeps its value for the next row.
Total letters: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i and watch how code advances across the whole triangle.
i | code before row | Printed row | code after row |
|---|---|---|---|
1 | 'A' | A | 'B' |
2 | 'B' | B C | 'D' |
3 | 'D' | D E F | 'G' |
4 | 'G' | G H I J | 'K' |
5 | 'K' | K L M N O | 'P' |
Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2 (A through O).
Where this tiny pattern (and its running counter) shows up beyond the homework prompt.
Clearest demo that loop counters and printed values can be different variables.
Example: move code++ outside the inner loop and watch letters repeat.
Teach reset-per-row vs continuous-fill as a one-idea change.
Example: side-by-side A/AB/ABC vs A/BC/DEF.
Swap code for an integer counter to print 1, 2 3, 4 5 6, …
Example: start n = 1 and print/increment the same way.
Add/remove spaces, or print commas, without changing the sequence logic.
Example: Example 3 drops spaces for a compact fill.
Triangular totals make O(n²) concrete for beginners.
Example: 5 rows → 15 letters (A–O).
Pair the pattern with a “stop at Z” or wrap policy.
Example: break when code > "Z".charCodeAt(0).
Pro Tip: say “one counter walks the alphabet; the outer loop only chooses how many to print” before coding - that story prevents resetting code each row.
Why this pattern earns a spot after the repeating-letter triangles.
Wrong increment placement shows up immediately as repeated letters.
Only loops, one extra char, and console output.
Swap letters for digits or remove spaces with tiny edits.
Streaming output needs no storage beyond loop counters and code.
Pro Tip: keep code outside the outer loop; resetting it each row accidentally recreates Program 1’s shape with a different letter rule.
Small habits that keep sequential-pattern code clean.
Use code or nextLetter for the sequence - keep i/j for row/column.
Avoid crashes when the user types letters instead of a number.
Put code++ inside the inner loop, after printing.
Six rows use 21 letters; seven need 28 - past Z unless you wrap/stop.
Trace rows = 3 (A / B C / D E F) on paper before coding larger demos.
Pro Tip: if every row starts with A, you almost certainly reset code inside the outer loop.
Mistakes that commonly break sequential alphabet patterns.
code Each RowSetting code = "A".charCodeAt(0) inside the outer loop recreates a reset-style triangle.
→ Declare and initialize code once, before the outer loop.
Moving code++ after the inner loop repeats the same letter across the row.
→ Increment inside the inner loop after each print.
Each letter lands on its own line - you get a column, not a triangle.
→ Use line += ch for letters; console.log(line) only after the inner loop.
Non-numeric input yields NaN with bare parseInt(prompt()).
→ Validate parseInt(prompt()) with Number.isFinite and validate range.
Large rows walk past 'Z' into non-letter characters.
→ Cap rows or stop when code > "Z".charCodeAt(0) for A–Z demos.
Check these inputs before calling the solution done.
Output is just A on one line.
Treat as invalid; re-prompt instead of silent empty output.
21 letters (A–U). Still inside A–Z.
28 letters needed - define wrap/stop policy.
Non-numeric input becomes 0 - check Number.isFinite first.
Same loops work with code = "a".charCodeAt(0).
Try these variations to lock in the pattern.
codecode > "Z".charCodeAt(0)n(n+1)/2 - hence O(n²) time.code outside the outer loop; increment it inside the inner loop.Z before accepting large row counts.Quick Takeaway: outer loop picks the width, running code supplies consecutive letters, then break the line - that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1-3) | O(rows²) | O(1) |
The sequential alphabet triangle is a small nested-loop exercise with lasting payoff: continuous state across rows, line += ch vs console.log(line), and O(n²) intuition. Master the running-code version, then optionally drop spaces for a compact fill.
Practice the three examples above, then continue to Program 14’s odd-length alphabet rows.
Keep code outside the outer loop, increment it per cell, and decide what happens after Z before accepting large row counts.
code once before the outer loopcode inside the inner loop after each printline += String.fromCharCode(code) for letters and console.log(line) after each rowNumber.isFinite after prompt()code = "A".charCodeAt(0) on every outer iterationconsole.log(line) inside the inner letter loopPrint the sequential triangle the beginner-friendly way.
Continuous letters, growing width
DefinitionNever reset between rows
CodePrint then code++
CodeEnds each row
I/OO(n²) time
AnalysisUnlike 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.
Next up: odd-length rows (A, ABC, ABCDE, …) stepping the end letter by two.
12 people found this page helpful