Wrap After Reaching E

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
ABCDE
BCDEA
CDEBA
DECBA
EDCBANode.js / console version
First loop prints i..E, second loop prints (i-1)..A.
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);
}Browser version (document.write)
Matches the reference: write letters directly and add a line break.
<!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
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.
Loops + condition
An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.
Connect it to the output
Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.
Key Takeaways
Each row prints 5 letters total.
First loop prints i..E.
Second loop wraps with (i-1)..A.
Time complexity: O(n²).
❓ Frequently Asked Questions
" " after each character in both loops, or build an array and join with a space.More JavaScript alphabet patterns
Browse the full list for more variations.
Patterns like this are a good way to practice composing a row from two loops without duplicating the “pivot” character.
10 people found this page helpful
