Reverse to-A Rows in JavaScript

What You'll Learn
The first row prints E down to A (EDCBA). Each next row drops the highest letter: DCBA, CBA, BA, then A. The outer loop walks E → A; the inner loop counts j from the current i down to A.
Total letters printed is \(5 + 4 + 3 + 2 + 1 = 15 = n(n+1)/2\) for five rows.
⭐ Pattern Output
Five rows as i moves from E down to A:
EDCBA
DCBA
CBA
BA
ANode.js / console version
Outer loop decreases i from end to start; inner loop runs j from i down to start:
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);
for (let i = end; i >= start; i--) {
let line = "";
for (let j = i; j >= start; 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 = i; j >= 65; j--) {
document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: high letter of the row
for (let i = end; i >= start; i--) sets the first (largest) character on each row: E, then D, …, finally A.
iInner loop: down to A
for (let j = i; j >= start; j--) prints backward along the alphabet from i to A.
i..ALine break
After each row, console.log(line) or document.write("<br>").
Not Program 2
Program 2 fixes the left at E and extends right; here the right anchor is A and the left moves. Complexity O(n²).
💡 Tips for Enhancement
Try These
Avoid
- Using
jfromEdown toi— that is Program 2’s growing pattern, not this one document.writeafter the document has loaded
Key Takeaways
Outer loop: E → A; inner loop: i → A (descending).
Each row is a reverse run ending at A.
Total letters: \(n(n+1)/2\).
Same outer direction as Program 2; different inner range.
Time complexity: O(n²).
❓ Frequently Asked Questions
j from E down to i, so rows grow: E, ED, EDC, … Program 7 uses j from i down to A, so rows shrink: EDCBA, DCBA, …i to E with i increasing. Program 7 walks backward from i to A with i decreasing from E.n rows from A to E.Next: JavaScript Alphabet Pattern 8
Continue to the next program in the alphabet pattern series.
Program 2 and Program 7 both use an outer loop from E down to A. The inner loop is what changes: E → i grows rows left-to-right from E; i → A (descending) shrinks reverse rows ending at A.
10 people found this page helpful
