Repeated-Letter Rows

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:
A
BB
CCC
DDDD
EEEEENode.js / console version
Outer loop picks the letter i; inner loop repeats it—note fromCharCode(i), not j:
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);
}Browser version (document.write)
Same logic with ASCII codes 65–69. Save as .html or use the live editor:
<!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
Outer loop: which letter
for (let i = start; i <= end; i++) selects the character code for the whole row: A, then B, …, E.
iInner loop: how many times
for (let j = start; j <= i; j++) runs i - start + 1 times. The body always uses i for the character.
Line break
After the inner loop, console.log(line) or document.write("<br>").
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 - iin a descending outer loop for an inverted repeat triangle
Avoid
- Printing
String.fromCharCode(j)here — that becomes Program 1’s growing alphabet document.writeafter the document has loaded
Key Takeaways
Outer i picks the letter; inner j only counts repetitions.
Row k (starting at 1) has length k and one repeated character.
Total characters: \(n(n+1)/2\).
Classic “trap”: same loop bounds as Program 1, different print expression.
Time complexity: O(n²).
❓ Frequently Asked Questions
j from start to i matches many textbook patterns. In production code, repeat(i - start + 1) is often clearer.ABCDE-style rows where each new character comes from j. Program 9 repeats a single character from i across the row.n rows from A to E.Next: JavaScript Alphabet Pattern 10
Continue to the next program in the alphabet pattern series.
Changing only the print expression from fromCharCode(j) to fromCharCode(i) turns the Program 1–style nested loops into this repeat-letter triangle.
10 people found this page helpful
