Consecutive Letters Pyramid (A to O)

What You'll Learn
This pattern prints a right-aligned pyramid where letters continue across rows (A to O). The trick is a global counter k that starts at A and increments each time a letter is printed.
⭐ Pattern Output
A
B C
D E F
G H I J
K L M N ONode.js / console version
In Node, we can use spaces for blanks and build each line as a string.
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
let k = A;
for (let i = A; i <= top; i++) {
let line = "";
for (let j = top; j >= A; j--) {
if (j > i) line += " ";
else line += String.fromCharCode(k++) + " ";
}
console.log(line.trimEnd());
}Browser version (document.write + div)
Matches the reference: use fixed-width cells for alignment and a global k that increments.
<!DOCTYPE html>
<html>
<head>
<style>
div.cell {
display: inline-block;
text-align: center;
width: 18px;
}
</style>
</head>
<body>
<script>
const A = 65;
const top = 69;
let k = A;
for (let i = A; i <= top; i++) {
for (let j = top; j >= A; j--) {
if (j > i) document.write(\"<div class=\\\"cell\\\"></div>\");
else document.write(\"<div class=\\\"cell\\\">\" + String.fromCharCode(k++) + \"</div>\");
}
document.write(\"<br>\");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: i from A to E
Row i contains 1, 2, 3, 4, then 5 letters (right-aligned).
Scan columns: j from E to A
The loop walks five fixed positions; early positions become blanks until j <= i.
Global counter: k++
When j <= i, print String.fromCharCode(k++) so letters continue across rows (A..O).
Right-aligned stream
Blanks shift the letters to the right, while k keeps the alphabet flowing.
Key Takeaways
Use a global k to keep letters continuous across rows.
Print blanks while j > i to right-align.
There are \(1+2+3+4+5=15\) letters total (A through O).
Time complexity: O(n²).
❓ Frequently Asked Questions
O.top to "F".charCodeAt(0) (or 70). You’ll print \(1+2+3+4+5+6=21\) letters (A through U). ) to keep alignment.More JavaScript alphabet patterns
Keep practicing nested loops with the next programs.
The count of letters printed after \(n\) rows is the triangular number \(n(n+1)/2\).
10 people found this page helpful
