Consecutive Letters (Shrinking Rows)

What You'll Learn
This pattern prints consecutive letters across rows while each row gets shorter. We keep a global counter k that starts at A, then print 5 letters on the first row, 4 on the second, and so on until 1 letter on the last row.
⭐ Pattern Output
A B C D E
F G H I
J K L
M N
ONode.js / console version
Use a global k and print letters with spaces. The inner loop length shrinks each row.
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 >= i; j--) {
line += String.fromCharCode(k++) + " ";
}
console.log(line.trimEnd());
}Browser version (document.write)
Matches the reference: write each letter plus a space, then a line break.
<!DOCTYPE html>
<html>
<body>
<script>
const A = 65;
const top = 69;
let k = A;
for (let i = A; i <= top; i++) {
for (let j = top; j >= i; j--) {
document.write(String.fromCharCode(k++) + " ");
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Core idea
We keep a global counter k starting at A (65). Outer i runs from A to E. Inner loop runs from E down to i, printing one letter per step and incrementing k. The row length shrinks by 1 each line, so we print 5+4+3+2+1 = 15 letters (A through O).
Loops + condition
An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.
Connect it to the output
Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.
Key Takeaways
Use k++ to keep letters continuous across rows.
Row length shrinks because the inner loop runs from E down to i.
5+4+3+2+1 = 15 letters (A..O).
Time complexity: O(n²).
❓ Frequently Asked Questions
O.top to "F".charCodeAt(0) (or 70). You’ll print \(6+5+4+3+2+1=21\) letters (A through U).+ " " part in both examples to print letters tightly.More JavaScript alphabet patterns
Browse the full list for more variations.
The total letters printed after \(n\) rows of a shrinking triangle is \(n(n+1)/2\), which is why 5 rows give 15 letters.
10 people found this page helpful
