Suffix-to-E Alphabet Rows in JavaScript

What You'll Learn
The outer index moves from E down to A. On each row, the inner loop prints forward from that letter through E. Row one is just E; the last row is the full A–E run.
Total letters printed is still \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\) for n rows.
⭐ Pattern Output
For five rows (E down to A):
E
DE
CDE
BCDE
ABCDENode.js / console version
Build each row with an inner loop from i to end, then console.log:
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);
for (let i = end; i >= start; i--) {
let line = "";
for (let j = i; j <= end; j++) {
line += String.fromCharCode(j);
}
console.log(line);
}Browser version (document.write)
Classic HTML page that writes as it parses. Save as .html or open in the live editor:
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 69; i >= 65; i--) {
for (let j = i; j <= 69; j++) {
document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: move the left edge
for (let i = end; i >= start; i--) sets the first character of the row: E, then D, …, down to A.
iInner loop: print through E
for (let j = i; j <= end; j++) walks codes upward so the row reads left-to-right along the alphabet until E.
jLine break
After the inner loop finishes one row, use console.log or document.write("<br>").
Growing forward runs
Each row is one character longer than the previous. Work is O(n²) for n rows, same total letters as Programs 1 and 2.
💡 Tips for Enhancement
Try These
- Compare with right-aligned star triangle (similar “widening” idea)
- Print the same pattern using
String.sliceon"ABCDE" - Change
endto another letter and watch the row widths adjust
Avoid
- Swapping inner bounds by mistake (
jmust run up toend) - Using
document.writeafter load in production pages
Key Takeaways
Outer loop decreases; inner loop increases—classic mixed-direction nested loops.
Row i prints characters with codes i … end.
Total output length: \(n(n+1)/2\) letters.
Contrasts with Program 2, which prints backward from E to i.
Time complexity: O(n²).
❓ Frequently Asked Questions
E, so you get DE when i is D, and so on.A on the left. Here the left edge moves while the right edge stays at E.n rows between A and E.Next: JavaScript Alphabet Pattern 4
Continue to the next program in the alphabet pattern series.
If you reverse each row string, you get the rows of Program 2 for the same A–E range—same loops, different print order.
10 people found this page helpful
