Decreasing Alphabet Rows in JavaScript

What You'll Learn
The first row prints A through E. Each next row lowers the last letter: ABCD, then ABC, until only A remains. The outer loop controls that shrinking cap; the inner loop always walks from A forward.
Total letters printed is \(5 + 4 + 3 + 2 + 1 = 15 = n(n+1)/2\) for five rows.
⭐ Pattern Output
Five rows from E down to A as the row cap:
ABCDE
ABCD
ABC
AB
ANode.js / console version
Outer loop decreases i from end to start; inner loop runs j from start to i:
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);
for (let i = end; i >= start; i--) {
let line = "";
for (let j = start; j <= i; j++) {
line += String.fromCharCode(j);
}
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 = 69; i >= 65; i--) {
for (let j = 65; j <= i; j++) {
document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: shrink the row cap
for (let i = end; i >= start; i--) sets how far each row extends: first E, then D, …, finally A.
iInner loop: always from A
for (let j = start; j <= i; j++) prints the forward alphabet from A through the current i.
jLine break
After each row, console.log(line) or document.write("<br>").
Inverted Program 1
Same character total as the growing triangle. Complexity O(n²) for n rows from A to E.
💡 Tips for Enhancement
Try These
- Compare with inverted star triangle (same outer idea: wide row first)
- Rebuild each row with
String.fromCharCodeinsideArray.from({ length }) - Revisit Program 1 side by side with this page
Avoid
- Incrementing the outer loop by mistake (you need
i--) document.writeafter the document has loaded
Key Takeaways
Outer loop goes E → A; inner loop goes A → i.
Each row is a prefix of ABCDE.
Total letters: \(n(n+1)/2\).
Mirrors Program 1’s row lengths in reverse order.
Time complexity: O(n²).
❓ Frequently Asked Questions
ABCDE; Program 5 starts with it. The multiset of printed characters is the same.A on the left.n rows between A and E.Next: JavaScript Alphabet Pattern 6
Continue to the next program in the alphabet pattern series.
If you listed Program 1’s rows from bottom to top, you would read the same lines as Program 5 for the same letter range.
10 people found this page helpful
