Continuous Letter Triangle

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Global k counter

What You'll Learn

The nested loops look like Program 1, but the printed character does not come from j. A third variable k starts at A and increments after every output, so the alphabet continues row to row until O on the last row.

Total letters: \(1 + 2 + 3 + 4 + 5 = 15 = n(n+1)/2\) — codes 65 (A) through 79 (O).

⭐ Pattern Output

Letters separated by spaces (as in the reference); five rows for AE as outer bounds:

Output
A 
B C 
D E F 
G H I J 
K L M N O
1

Node.js / console version

Build each row with an array and join(" ") so the console line has no trailing space:

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

let k = start;
for (let i = start; i <= end; i++) {
  const parts = [];
  for (let j = start; j <= i; j++) {
    parts.push(String.fromCharCode(k++));
  }
  console.log(parts.join(" "));
}
2

Browser version (document.write)

Matches the classic snippet: space after each character inside the inner loop:

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

🧠 How It Works

1

Outer loop: row size

for (let i = start; i <= end; i++) sets how many letters appear on the row: 1, 2, …, n.

Same as Program 1
2

Inner loop: count only

for (let j = start; j <= i; j++) runs once per letter on this row. j is not used in fromCharCode.

Iteration count
3

Global k

String.fromCharCode(k++) prints the next letter and advances k for the whole program.

Continuous stream
=

Not Program 1

Program 1’s first two rows would be A and AB; here they are A and B C. Complexity O(n²).

💡 Tips for Enhancement

Try These

  • Extend end and confirm k ends at the expected letter (triangular number formula)
  • Replace spaces with commas or HTML non-breaking spaces for layout experiments
  • Trace k on paper for two rows to see why j is unused in fromCharCode

Avoid

  • Resetting k inside the outer loop (that would restart at A every row)
  • document.write after the document has loaded

Key Takeaways

1

Nested loops match Program 1; the stream uses k++, not j.

2

Fifteen letters for five rows: A through O.

3

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

4

Spaces separate letters on each row in the HTML version.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

Any loop that runs i - start + 1 times per row works (e.g. let t = 0; t < i - start + 1; t++). Matching j to start..i mirrors Program 1 and is easy to read.
After the last print, k is 80 (one past O). The last character shown is O (code 79).
O(n²) for n rows between A and E.

Next: JavaScript Alphabet Pattern 14

Continue to the next program in the alphabet pattern series.

Program 14 →
Did you know?

The number of letters printed is the fifth triangular number, 15, so the last letter is the 15th letter of the alphabet, O.

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