JavaScript Repeating Alphabet Triangle Pattern

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

What Is This Pattern?

A repeating alphabet triangle grows by one character each row, but every character on that row is the same letter — the letter advances with the row.

Remember
Rule: for letter i from A to last,
      print i with growing width 1, 2, 3, …

A
BB
CCC
DDDD
EEEEE     ← 5 rows

Unlike Program 1 (A, AB, ABC), letters do not step across the row — you append the outer loop letter inside the inner loop. The reverse twin is Program 10 (E, DD, CCC, …).

How to Solve It

Two ways to emit the same shape — start with nested loops, then optionally shorten with String.repeat.

MethodIdeaBest for
Nested loopsOuter = letter; inner = growing width; append outer letterLearning, interviews, exams
ch.repeat(row)Build a whole repeated-letter row in one callShorter demos once loops click

Pseudocode

Pseudocode
base = code of 'A'
for row from 1 to rows:
    ch = fromCharCode(base + row - 1)
    line = ""
    for j from 1 to row:
        append ch to line
    print line

Cheat sheet

GoalPattern
Advance letterfor (let row = 1; row <= rows; row++)
Letter from rowconst ch = String.fromCharCode("A".charCodeAt(0) + row - 1);
Grow widthfor (let j = 1; j <= row; j++) line += ch;
Uniform rowAppend ch (outer letter), not the inner index
End the rowconsole.log(line);
One-line shortcutconsole.log(ch.repeat(row));
Reverse twinProgram 10 (E, DD, CCC, …)

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same lineEach letter on the row
console.log(line)Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the repeating 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 result 5 rows · 15 letters
A
BB
CCC
DDDD
EEEEE

Worked Walkthrough — rows = 4

Trace each outer-loop letter as the row advances from 1 to 4.

LetterInner runsPrinted rowRepeats
A1A1
B2BB2
C3CCC3
D4DDDD4

Total letter prints: 1 + 2 + 3 + 4 = 10 = 4×5/2 — same triangular count as Program 1.

JavaScript Programs

Three complete programs: fixed last letter, prompt input, and a repeat 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 picks the letter; inner loop only controls the repeat count.

JavaScript
let rows = 5;
const base = "A".charCodeAt(0);

for (let row = 1; row <= rows; row++) {
  const ch = String.fromCharCode(base + row - 1);
  let line = "";
  for (let j = 1; j <= row; j++) {
    line += ch;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop picks the row letter. Row 1 → A, row 2 → B, … up through E.

2. Inner loop only counts. j runs from 1 to row, so the width grows 1, 2, 3, …

3. Append ch, not a stepping letter. That keeps every character on the row the same. Stepping codes would rebuild Program 1.

When row = 3 the inner loop runs three times and you get CCC.

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 base = "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 row = 1; row <= rows; row++) {
    const ch = String.fromCharCode(base + row - 1);
    let line = "";
    for (let j = 1; j <= row; j++) {
      line += ch;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).

2. Map row index to a letter. fromCharCode(base + row - 1) — for row = 3, ch is C.

3. Repeat ch exactly row times. Same uniform-row idea as Example 1.

Example 3 — ch.repeat(row)

Build each repeated-letter row in one call — same shape, no explicit inner letter loop.

JavaScript
let rows = 5;
const base = "A".charCodeAt(0);

for (let row = 1; row <= rows; row++) {
  const ch = String.fromCharCode(base + row - 1);
  console.log(ch.repeat(row));
}
Try It Yourself

How It Works

1. Pick the letter from the row index. Same mapping as Example 2: row 1 → A, row 2 → B, and so on.

2. Fill a string of length row. ch.repeat(row) creates that string — e.g. "C".repeat(3) is CCC.

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.

Wrong letter

Program 1 by mistake

Appending a stepping code instead of the row letter produces A, AB, ABC. Always append the outer-loop letter (ch).

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.

Off-by-one

Wrong letter mapping

Use base + row - 1. Forgetting - 1 shifts every letter one step forward.

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. A good sanity check for the mapping and the break.

Bad prompt

Use Number.isFinite

Bare parseInt(prompt()) yields NaN on letters — validate before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(n) for the current line string
ch.repeat(row) (Example 3)O(rows²)O(n) per temporary row string

Total letters = 1 + 2 + … + n = n(n + 1)/2 — quadratic in n. Same totals as Program 1.

Key Takeaways

  • Rule: outer loop picks the letter; inner loop only sets how many times to append it.
  • Append ch, not a stepper: that single choice separates this pattern from Program 1.
  • Break the row: call console.log only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count.

One line: for each row letter, append that letter exactly as many times as the row width, then console.log.

Frequently Asked Questions

Because the inner loop appends the outer-loop character every time. The inner counter only controls how many times to append, not which character to print.
Then letters would change across the row (A, AB, ABC…), which is Program 1 — not a repeating-letter triangle.
On the third row the row letter is C, and the inner loop runs three times, appending C each time.
Program 1 prints stepping letters across each row (A, AB, ABC). This pattern keeps one letter per row and only grows the repeat count (A, BB, CCC).
Program 10 uses the same growing widths but letters count downward (E, DD, CCC…). This pattern counts upward (A, BB, CCC…).
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. console.log(ch.repeat(row)) prints a full repeated-letter row in one call. Nested loops are better for learning; repeat is a handy shortcut later.

Did you know?

Each row prints the same letter repeatedly: row 1 prints A once, row 2 prints B twice, row 3 prints C three times. Print the outer loop letter inside the inner loop so the row stays uniform. The reverse twin is Program 10.

Next: Reverse Repeating Triangle

Same growing widths, letters counting down from E.

Program 10 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