JavaScript Sequential Alphabet Triangle Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Keep one shared code outside the outer loop. Optionally put spaces between letters for readability.

MethodIdeaBest for
Nested loops + spacesOuter = row width; inner appends letter, optional space, then code++Learning, readable demos
Nested loops, no spacesSame continuous sequence, denser rowsCompact console output

Pseudocode

Pseudocode
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

GoalPattern
Start oncelet code = "A".charCodeAt(0); (outside the outer loop)
Walk each rowfor (let i = 1; i <= rows; i++)
Append + advanceline += String.fromCharCode(code); code++;
Space between lettersif (j < i) line += " ";
End the rowconsole.log(line);
Letter totaln(n+1)/2 — keep ≤ 26 for A–Z only
Resetting twinEach row starts at A → Program 1

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter (and optional space)
console.log(line)Ends the current rowAfter the inner loop

Append letters without a newline, then end the row once.

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 result 5 rows · last O · 15 letters
A
B C
D E F
G H I J
K L M N O

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and the letters taken from the running code.

iLetters usedPrinted rowCount
1AA1
2B, CB C2
3D, E, FD E F3
4G, H, I, JG H I J4

Total letter characters: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).

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);
}
Try It Yourself

How It Works

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);
  }
}
Try It Yourself

How It Works

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);
}
Try It Yourself

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Odd-Length Alphabet Triangle

Row widths 1, 3, 5… — A, then ABC, then ABCDE…

Program 14 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful