Continuous Letter Triangle

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 A–E as outer bounds:
A
B C
D E F
G H I J
K L M N ONode.js / console version
Build each row with an array and join(" ") so the console line has no trailing space:
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(" "));
}Browser version (document.write)
Matches the classic snippet: space after each character inside the inner loop:
<!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
Outer loop: row size
for (let i = start; i <= end; i++) sets how many letters appear on the row: 1, 2, …, n.
Inner loop: count only
for (let j = start; j <= i; j++) runs once per letter on this row. j is not used in fromCharCode.
Global k
String.fromCharCode(k++) prints the next letter and advances k for the whole program.
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
endand confirmkends at the expected letter (triangular number formula) - Replace spaces with commas or HTML non-breaking spaces for layout experiments
- Trace
kon paper for two rows to see whyjis unused infromCharCode
Avoid
- Resetting
kinside the outer loop (that would restart atAevery row) document.writeafter the document has loaded
Key Takeaways
Nested loops match Program 1; the stream uses k++, not j.
Fifteen letters for five rows: A through O.
Total characters: \(n(n+1)/2\).
Spaces separate letters on each row in the HTML version.
Time complexity: O(n²).
❓ Frequently Asked Questions
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.k is 80 (one past O). The last character shown is O (code 79).n rows between A and E.Next: JavaScript Alphabet Pattern 14
Continue to the next program in the alphabet pattern series.
The number of letters printed is the fifth triangular number, 15, so the last letter is the 15th letter of the alphabet, O.
10 people found this page helpful
