Shrinking Repeat Rows

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Inner E → i

What You'll Learn

Each row repeats a single letter, but unlike Program 9 (lengths 1 … 5), lengths here go 5 … 1. The inner loop matches Program 10 (j from E down to i); only the outer loop direction differs—A through E instead of E down to A.

Total characters: \(5 + 4 + 3 + 2 + 1 = 15 = n(n+1)/2\) for five rows.

⭐ Pattern Output

Five rows from A to E:

Output
AAAAA
BBBB
CCC
DD
E
1

Node.js / console version

Outer loop increases i from start to end; inner loop runs j from end down to i, printing fromCharCode(i) each time:

JavaScript
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);

for (let i = start; i <= end; i++) {
  let line = "";
  for (let j = end; j >= i; j--) {
    line += String.fromCharCode(i);
  }
  console.log(line);
}
2

Browser version (document.write)

Same logic with ASCII codes 6569. Save as .html or use the live editor:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 65; i <= 69; i++) {
  for (let j = 69; j >= i; j--) {
    document.write(String.fromCharCode(i));
  }
  document.write("<br>");
}
</script>
</body>
</html>

🧠 How It Works

1

Outer loop: row letter

for (let i = start; i <= end; i++) moves from A up to E. That letter fills the row.

Ascending i
2

Inner loop: repeat count

for (let j = end; j >= i; j--) runs end - i + 1 times—the same inner pattern as Program 10.

Descending j
3

Line break

After the inner loop, console.log(line) or document.write("<br>").

New row
=

Same lengths as Program 11

Top-to-bottom lengths are 5,4,3,2,1 like Program 11, but letters run A → E instead of E → A. Complexity O(n²).

💡 Tips for Enhancement

Try These

  • Place Program 10 beside this page: same inner loop, opposite outer direction
  • One-liner row: String.fromCharCode(i).repeat(end - i + 1)
  • Contrast lengths with Program 9 (1 … 5 vs 5 … 1)

Avoid

  • Using inner j from A to i here — that is Program 9 / Program 11 style, not this pattern
  • document.write after the document has loaded

Key Takeaways

1

Outer i: A → E; inner j: E → i.

2

Row lengths top-to-bottom: 5, 4, 3, 2, 1.

3

Total characters: \(n(n+1)/2\).

4

Inner loop matches Program 10; outer matches Program 9’s direction.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

No. Reversing Program 9 gives EEEEE, DDDD, … (Program 11). Program 12 keeps lengths 5 … 1 but uses letters A … E on each row.
Same inner loop. Program 10’s outer i goes E → A (short row first). Program 12’s outer i goes A → E (long A row first).
O(n²) for n rows from A to E.

Next: JavaScript Alphabet Pattern 13

Continue to the next program in the alphabet pattern series.

Program 13 →
Did you know?

Swapping only the outer loop between Program 10 and Program 12 swaps whether the shortest or longest repeated row prints first.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful