Repeated-Letter Rows

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Same char per row

What You'll Learn

Row lengths grow 1, 2, 3, 4, 5, but every character on a row is the same letter—the one for that outer i. The inner j is only a counter from A’s code up to i; the value printed is always String.fromCharCode(i), not j.

Total characters: \(1 + 2 + 3 + 4 + 5 = 15 = n(n+1)/2\) for five rows.

⭐ Pattern Output

Five rows from A to E:

Output
A
BB
CCC
DDDD
EEEEE
1

Node.js / console version

Outer loop picks the letter i; inner loop repeats it—note fromCharCode(i), not j:

JavaScript
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);

for (let i = start; i <= end; i++) {
  let line = "";
  for (let j = start; j <= i; j++) {
    line += String.fromCharCode(i);
  }
  console.log(line);
}
2

Browser version (document.write)

Same logic with ASCII codes 6569. Save as .html or use the live editor:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 65; i <= 69; i++) {
  for (let j = 65; j <= i; j++) {
    document.write(String.fromCharCode(i));
  }
  document.write("<br>");
}
</script>
</body>
</html>

🧠 How It Works

1

Outer loop: which letter

for (let i = start; i <= end; i++) selects the character code for the whole row: A, then B, …, E.

Row letter i
2

Inner loop: how many times

for (let j = start; j <= i; j++) runs i - start + 1 times. The body always uses i for the character.

Repeat count
3

Line break

After the inner loop, console.log(line) or document.write("<br>").

New row
=

Not Program 1

Program 1 would print String.fromCharCode(j) so each position advances the letter. Complexity O(n²).

💡 Tips for Enhancement

Try These

  • Rewrite a row as String.fromCharCode(i).repeat(i - start + 1) (same result, no inner loop)
  • Compare with Program 1 in two editor tabs
  • Use end - i in a descending outer loop for an inverted repeat triangle

Avoid

  • Printing String.fromCharCode(j) here — that becomes Program 1’s growing alphabet
  • document.write after the document has loaded

Key Takeaways

1

Outer i picks the letter; inner j only counts repetitions.

2

Row k (starting at 1) has length k and one repeated character.

3

Total characters: \(n(n+1)/2\).

4

Classic “trap”: same loop bounds as Program 1, different print expression.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

For learning nested loops, counting j from start to i matches many textbook patterns. In production code, repeat(i - start + 1) is often clearer.
Program 1 prints ABCDE-style rows where each new character comes from j. Program 9 repeats a single character from i across the row.
O(n²) for n rows from A to E.

Next: JavaScript Alphabet Pattern 10

Continue to the next program in the alphabet pattern series.

Program 10 →
Did you know?

Changing only the print expression from fromCharCode(j) to fromCharCode(i) turns the Program 1–style nested loops into this repeat-letter triangle.

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.

10 people found this page helpful