Inverted Triangle Alphabet Pattern in JavaScript

What You'll Learn
The outer loop moves the “start” of each row from E down to A. The inner loop prints from E down to that letter, so rows grow longer as you go down—the mirror of Program 1’s shape.
Total letters for n rows is still \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\).
⭐ Pattern Output
When you run the program for five rows (from E down to A):
E
ED
EDC
EDCB
EDCBANode.js / console version
Uses let, builds each row in a string, and prints with console.log:
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);
for (let i = end; i >= start; i--) {
let line = "";
for (let j = end; j >= i; j--) {
line += String.fromCharCode(j);
}
console.log(line);
}Browser version (document.write)
Classic tutorial style: write into the document as the page loads. Save as .html and open in a browser, or use the live editor:
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 69; i >= 65; i--) {
for (let j = 69; j >= i; j--) {
document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: row anchor
for (let i = end; i >= start; i--) picks the smallest letter printed on the current row (from E down to A).
iInner loop: print the suffix
for (let j = end; j >= i; j--) prints letters from E down to i, which matches the reference pattern.
jNew line after each row
Use console.log(line) in Node, or document.write("<br>") in HTML.
Inverted rows
Same total work as Program 1: O(n²) character writes for n rows. Each row is a reverse run from E to the current i.
💡 Tips for Enhancement
Try These
- Swap
startandendto change how many rows you print - Compare with inverted star triangle (same loop idea)
- Build the row with
Array.fromand.join("")for a functional style
Avoid
- Using
document.writeafter the document has finished loading - Off-by-one mistakes on the inner bound (
j >= i)
Key Takeaways
Both loops move downward in letter value; each row is a suffix E..i.
69 is 'E', 65 is 'A' in ASCII.
Total letters: \(n(n+1)/2\) for n rows.
This is the letter version of an inverted right triangle.
Time complexity: O(n²).
❓ Frequently Asked Questions
A upward. Program 2 fixes the high letter at E and extends leftward each row by lowering the minimum letter from E to A."EDCBA" or build substrings, but numeric codes match the classic nested-loop teaching style.n rows: the inner loop length increases each time.Next: JavaScript Alphabet Pattern 3
Continue to the next program in the alphabet pattern series.
Decrementing j from end to i is the same structure as counting stars in an inverted triangle—only the character source changes.
10 people found this page helpful
