Wrap After Reaching E

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two loops per row

What You'll Learn

This pattern prints letters from the row start up to E, then wraps back toward A to complete the row. Each row has exactly 5 letters.

⭐ Pattern Output

Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
1

Node.js / console version

First loop prints i..E, second loop prints (i-1)..A.

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

for (let i = A; i <= top; i++) {
  let line = "";
  for (let j = i; j <= top; j++) line += String.fromCharCode(j);
  for (let k = i; k > A; k--) line += String.fromCharCode(k - 1);
  console.log(line);
}
2

Browser version (document.write)

Matches the reference: write letters directly and add a line break.

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

🧠 How It Works

1

Core idea

Outer i runs from A to E. First inner loop prints i..E. Second loop prints the wrap part by counting down from i to just above A and printing k-1 (so it prints i-1..A). This produces rows like BCDEA and ends at EDCBA.

Logic
2

Loops + condition

An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.

Nested loops
=

Connect it to the output

Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.

Key Takeaways

1

Each row prints 5 letters total.

2

First loop prints i..E.

3

Second loop wraps with (i-1)..A.

4

Time complexity: O(n²).

❓ Frequently Asked Questions

Row 2 starts at B and prints up to E (BCDE). The wrap loop then prints A to finish the 5th character.
Yes. Add " " after each character in both loops, or build an array and join with a space.
For \(n\) rows, each row prints \(n\) letters, so it’s O(n²).

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 27 →
Did you know?

Patterns like this are a good way to practice composing a row from two loops without duplicating the “pivot” character.

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