Repeating-Letter Alphabet Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Row 1 is one A, row 2 is two Bs, row 3 three Cs, and so on: A, BB, CCC, DDDD, EEEEE. Contrast Program 1, where letters change inside each row. Here, the row letter stays the same and only the count grows. Next up: Program 10 reverses the letter order. Includes a live preview, worked JavaScript examples, edge cases, and complexity.

Shape Rule

One letter, growing count

Row 1 prints A, row 2 prints BB, row 3 prints CCC, row 4 prints DDDD, row 5 prints EEEEE.

Outer Loop

Row letter

for (let row = 1; row <= rows; row++) picks both the letter offset and how many times to repeat it on each line.

Inner Loop

Count only

for (let j = 0; j < row; j++) { line += ch; } appends the same letter row times — the inner loop only controls count, not which letter.

String Shortcut

ch.repeat(row)

console.log(ch.repeat(row)) repeats the letter in one line — same output as nested loops.

Live Preview

Rows 1–26

Pick a row count and draw A, BB, CCC live.

O(n²)

Complexity

1+2+…+n printed characters total.

Introduction

A repeating-letter alphabet pattern (A, BB, CCC, ...) prints one letter per row, repeated as many times as the row number. With five rows the console shows A, BB, CCC, DDDD, EEEEE — unlike Program 1, where letters change inside each row.

in JavaScript you solve it with two nested for loops: compute ch = String.fromCharCode("A".charCodeAt(0) + row - 1), repeat that letter row times with the inner loop, then call console.log(line) for the next line. Or shorten each row to console.log(ch.repeat(row)).

Why it matters?

It teaches that the inner loop can control repetition count while the outer loop picks the value — a key idea before Program 10’s reverse variant.

Key Highlights

Row letter

ch = String.fromCharCode("A".charCodeAt(0) + row - 1)

Inner loop

for (let j = 0; j < row; j++) — count only.

Print

line += ch then console.log(line).

Output

A, BB, CCC, …

In short: for each row from 1 to rows, set ch = String.fromCharCode("A".charCodeAt(0) + row - 1), append ch exactly row times with line += ch, then call console.log(line).

📝 Problem & Approach

Given a positive integer rows, print a left-aligned repeating-letter alphabet pattern: each row prints one letter repeated row times (A, BB, CCC when rows = 5).

JavaScript
// First 5 rows (conceptual shape)
// A
// BB
// CCC
// DDDD
// EEEEE

Inputs & Outputs

ItemTypeDescription
rows / topint / charNumber of rows; last letter is 'A' + rows - 1 (E for 5).
Printed outputtextGrowing rows of repeated letters A, BB, CCC, …

Minimal workflow

Pseudocode
base = "A".charCodeAt(0)
for row from 1 to rows:
    ch = String.fromCharCode(base + row - 1)
    repeat ch exactly row times (inner loop or ch.repeat(row))
    console.log(line)

Approach comparison

ApproachIdeaBest for
Char nested loopsOuter i++, inner count, print iMatching this classic sample
Row index + char mathch = (char)('A' + row - 1) then print row timesUser-input versions; clearer count

⚡ Quick Reference

GoalPattern
Walk each rowfor (let row = 1; row <= rows; row++):
Row letterch = String.fromCharCode("A".charCodeAt(0) + row - 1)
Repeat letterfor (let j = 0; j < row; j++) { line += ch; }
End the rowconsole.log(line)
One-line shortcutconsole.log(ch.repeat(row))
Stepping letters variantSee Program 1 — A, AB, ABC triangle
Reverse repeat variantSee Program 10 (E, DD, CCC, …)

📋 Nested loop vs ch.repeat(row)

Same A, BB, CCC shape — two ways to think about row letter and repetition count.

Nested loop
for (let j = 0; j < row; j++)

Classic charCode loop — teaches letter formula and repetition count separately

ch.repeat(row)
console.log(ch.repeat(row))

String.repeat — compact one-liner per row

Learning tip
loops first

Master nested loops before the string shortcut

Context

When This Pattern Shows Up

Reach for this when teaching that the inner loop can control count while the outer variable controls the printed value.

  1. After Program 1

    Keep the same loop bounds; change only line += String.fromCharCode(j) to line += String.fromCharCode(i).

  2. Repeat-count drills

    Practice decoupling “what to print” from “how many times.”

  3. Bridge to Program 10

    Next keeps repeats but walks the letter backward: E, DD, CCC.

  4. Index-to-char practice

    Map row numbers to letters with 'A' + row - 1.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: printing the outer letter inside the inner loop is the cleanest way to build a growing triangle of repeated characters.

🔮 Live Preview

Choose 1–26 rows and draw the repeating-letter alphabet triangle in the browser.

Try 5 (classic A…EEEEE) or 4 (A…DDDD). Cap is 26 letters (A–Z).

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs — fixed A–E, user-chosen row count, and a ch.repeat(row) shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.

📚 Getting Started

Print five growing rows of repeated letters from A to E.

Example 1 — Fixed rows = 5

Hard-coded height — ideal for first demos and screenshots.

JavaScript
const 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 = 0; j < row; j++) {
    line += ch;
  }
  console.log(line);
}
Try it Yourself

How It Works

When row = 1, ch is A and the inner loop appends it once. When row = 3, ch is C and the line becomes CCC. When row = 5, ch is E and the row is EEEEE. console.log(line) after the inner loop starts the next row.

📈 Practical Variant

Let the user choose how many rows to print.

Example 2 — User Input Version

Read the row count with prompt() and convert with parseInt() (validate with Number.isFinite in real apps).

JavaScript
const rowsInput = prompt("Enter the number of rows (max 26):");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 26));

const base = "A".charCodeAt(0);

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

How It Works

For row 4, ch becomes D and the inner loop appends it four times. Cap rows at 26 so ch stays within A–Z.

⚡ Shortcut Style

Same shape with JavaScript String.repeat.

Example 3 — ch.repeat(row) String Repetition

Repeat the row letter by the row number — same triangle, one log per row.

JavaScript
const rows = 5;
const clampedRows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);

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

How It Works

String.fromCharCode("A".charCodeAt(0) + row - 1).repeat(row) builds each line in one expression. Keep the nested-loop version for exams that ask you to show outer vs inner letter logic.

🧠 How the Algorithm Prints Rows

1

Outer loop selects the row letter

i runs from 'A' to top. That’s the character printed on the row.

Row character
2

Inner loop controls the repeat count

j runs from 'A' to i, so it executes 1, 2, 3, … times as rows grow.

1..n repeats
3

Append ch, not j

Appending ch (the row letter) keeps the whole row the same. Appending the inner counter would change letters across the row (Program 1).

Key idea
4

New line

console.log(line) ends each row before the next letter begins.

Line break
=

Same triangle shape

Total prints are 1+2+…+n for n rows, so time complexity is O(n²).

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop row value and see how the letter formula and repetition count produce each printed line.

rowLetterCountOutput
1A1A
2B2BB
3C3CCC
4D4DDDD
5E5EEEEE

Highlight rows 1, 3, and 5: A ×1 → A; C ×3 → CCC; E ×5 → EEEEE. Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.

Use Cases

Where this repeating-letter alphabet triangle shows up beyond the homework prompt.

1. Value vs Count Labs

Clearest demo of printing the outer variable inside the inner loop.

Example: change line += String.fromCharCode(i) to line += String.fromCharCode(j) and compare with Program 1.

2. Repeat Practice

Build intuition for loops that only control iteration count.

Example: rewrite the inner loop as for (int k = 0; k < n; k++).

3. Char Math

Map row indexes to letters with 'A' + row - 1.

Example: scale from 5 to 8 without rewriting loops.

4. String Helpers

Later rewrite as new string(ch, row) once the idea clicks.

Example: same output with one print per row.

5. Complexity Intuition

Triangle sums make O(n²) easy to see.

Example: 15 letters for 5 rows.

6. Series Continuity

Sits between Programs 8 and 10 in the alphabet set.

Example: revisit Program 1.

Pro Tip: say “pick the letter outside, repeat it inside” before coding — that story prevents printing j by habit.

Advantages

Why this pattern earns a spot early in the alphabet-pattern series.

  1. 1. Instant Visual Feedback

    A stepping-letter row (ABC) shows immediately if you printed j.

  2. 2. Tiny Diff from Program 1

    Only the printed variable changes.

  3. 3. Scales Cleanly

    Change the top letter or row count and the whole triangle grows.

  4. 4. Beginner-Friendly

    No padding or diagonal checks — just two loops and one print rule.

Pro Tip: master Program 1 first; this page is mostly “same loops, print the outer letter.”

Usage Tips

Small habits that keep repeating-letter alphabet triangles clean.

  1. 1. Always Print i (or ch)

    Printing j turns this into Program 1.

  2. 2. Let the Inner Loop Only Count

    Do not increment the character inside the inner loop.

  3. 3. Cap Rows at 26

    Keep the row letter inside A–Z when taking user input.

  4. 4. Validate prompt() input

    Validate the row count before using parseInt(prompt()).

  5. 5. Compute the Letter from the Row

    Use ch = (char)('A' + row - 1) when working with integer row indexes.

Pro Tip: if you see A, AB, ABC, you appended j — switch back to appending the row letter ch (or i).

Common Pitfalls

Mistakes that commonly break repeating-letter alphabet triangles.

  1. 1. Off-by-one in String.fromCharCode(base + row - 1)

    Using String.fromCharCode(base + row) skips A on row 1 or starts at B. Forgetting row - 1 shifts every letter forward.

    → Always use ch = String.fromCharCode(base + (row - 1)) with 1-based row values.

  2. 2. Starting the outer loop at 0

    A 0-based outer loop breaks String.fromCharCode(base + row - 1) unless you add extra +1 fixes — start at 1 so row matches both letter offset and repeat count.

    → Use for (let row = 1; row <= rows; row++): so row matches both letter offset and repeat count.

  3. 3. rows > 26 without clamping

    Row 27 would need a letter beyond ZString.fromCharCode() still returns a character but not the expected alphabet pattern.

    → Clamp with rows = Math.max(1, Math.min(rows, 26)) after reading input.

  4. 4. Printing inner counter letter (Program 1 shape)

    Printing a changing letter inside the inner loop produces A, AB, ABC — Program 1, not this pattern.

    → Print the same ch every time in the inner loop, or use console.log(ch.repeat(row)).

  5. 5. Forgetting console.log(line) after the inner loop

    Omitting console.log(line) glues every letter onto one endless line.

    → Always end the row after the inner loop (unless using console.log(ch.repeat(row)) alone).

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A.

rows = 5

Classic sample

A through EEEEE (Example 1).

rows = 4

Smaller triangle

Ends at DDDD (Example 2).

rows > 26

Past Z

Cap or reject — the row letter leaves the alphabet.

Bad input

Empty / non-numeric

Validate with Number.isFinite validation.

Lowercase

a, bb, ccc

Swap 'A' for 'a' as the base.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip to Program 1

  • Change only line += String.fromCharCode(i) to line += String.fromCharCode(j)
  • Confirm you get A, AB, ABC

2. Reverse the letters

  • Start the outer letter at E and count down
  • Continue to Program 10

3. Scale to 8 rows

  • Use the input version
  • Check the last row is HHHHHHHH

4. One print per row

  • Rewrite with new string(ch, row)
  • Confirm the output matches Example 2

Notes

  • Outer picks the letter. Inner only decides how many times to print it.
  • Printing i (not j) is what keeps each row uniform.
  • Row lengths are 1, 2, …, n — same geometry as Program 1.
  • Next up: Alphabet Pattern 10 reverses the letter order while keeping repeats.

Quick Takeaway: choose the row letter in the outer loop, then print that letter once per inner iteration — that alone builds A, BB, CCC, …

⏱️ Time and Space Complexity

ProgramTimeExtra space
Inline / input (Examples 1–2)O(n²)O(1)
ch.repeat(row) (Example 3)O(n²)O(1)

For n rows you print 1+2+…+n = n(n+1)/2 characters, so total work is O(n²).

Wrap Up

🎉 Conclusion

The repeating-letter alphabet triangle is Program 1 with a different print rule: the outer loop picks the letter, and the inner loop only repeats it. Master the classic A…EEEEE sample, then try user input and the multiplication shortcut.

Practice the three examples above, then continue to Alphabet Pattern 10.

Outer row letter from "A".charCodeAt(0) to top; inner loop counts repeats; append ch each time, then console.log(line) for the newline.

💡 Best Practices

✅ Do

  • Print the outer letter (i or ch) inside the inner loop
  • Use the inner loop only for the repeat count
  • Compute ch = (char)('A' + row - 1) for integer row indexes
  • Cap user row counts at 26
  • State O(n²) when asked about complexity

❌ Don’t

  • Print j when you want A, BB, CCC
  • Change the letter inside the inner loop
  • Forget the - 1 in the letter formula
  • Skip validating row-count input
  • Call console.log(line) inside the letter loop

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the repeating-letter alphabet triangle the beginner-friendly way.

5
Core concepts
= 02

Print

line += ch (row letter), not j

Code
1 03

Inner

Controls count only

Shape
R 04

vs Prog 1

Same loops, different print

Compare
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop walks row from 1 to rows. Each row picks one letter with ch = String.fromCharCode("A".charCodeAt(0) + row - 1) and the inner loop appends that letter row times. Row 1 logs A once, row 3 logs C three times as CCC, and row 5 logs E five times as EEEEE.
A 1-based row matches both the letter offset (row - 1) and the repetition count (row). Starting at 0 breaks the formula unless you add extra +1 fixes everywhere.
"A".charCodeAt(0) is 65. For row 1 you get fromCharCode(65) = A; for row 3 you get fromCharCode(67) = C. The row - 1 converts 1-based row numbers into 0-based letter offsets from A.
Yes. After computing ch, console.log(ch.repeat(row)) repeats the character row times in one expression. Nested loops teach repetition control; String.repeat is the compact JavaScript shortcut.
Program 1 logs ascending letters A, AB, ABC on each row. This pattern repeats a single letter per row — A, BB, CCC — so every character on a line is identical.
Program 9 grows from A downward: A, BB, CCC, DDDD. Program 10 mirrors it from the top letter backward — E, DD, CCC, BB, A when rows = 5.
O(n²) where n is the number of rows. Total logged characters equal 1+2+3+...+n = n(n+1)/2 — the same triangular total as Programs 1, 4, 5, 7, and 8.
Use parseInt with Number.isFinite after prompt(), then clamp rows between 1 and 26 so fromCharCode never exceeds Z.

Did you Know? 🔊

Each row logs one letter repeated row times: row 1 is A, row 2 is BB, row 3 is CCC. Letter = String.fromCharCode("A".charCodeAt(0) + row - 1). Compare Program 10 (reverse repeated letters) and Program 1 (classic A.. triangle).

Continue to Program 10

Reverse repeating triangle — the next alphabet pattern in the series.

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