Shifted Rows (ABC.. / BAB..)

What You'll Learn
This pattern mixes a small descending prefix with an ascending suffix. The formula 134 - i controls how far the ascending part goes so rows stay at length 5.
⭐ Pattern Output
ABCDE
BABCD
CBABC
DCBAB
EDCBANode.js / console version
First loop prints i..B, second loop prints A..(134-i).
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let line = "";
for (let j = i; j > A; j--) line += String.fromCharCode(j);
for (let k = A; k <= (A + top) - i; k++) line += String.fromCharCode(k);
console.log(line);
}Browser version (document.write)
Matches the reference logic using ASCII codes (with the clearer A + top - i form).
<!DOCTYPE html>
<html>
<body>
<script>
const A = 65;
const top = 69;
for (let i = A; i <= top; i++) {
for (let j = i; j > A; j--) document.write(String.fromCharCode(j));
for (let k = A; k <= (A + top) - i; k++) document.write(String.fromCharCode(k));
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Core idea
Outer i runs from A to E. First loop prints a descending prefix from i down to B (skipping A). Second loop prints the remaining letters from A to (134 - i). Since (134 = 69 + 65), this makes the last character shift left each row.
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
Prefix loop prints descending letters from the row start.
Suffix loop prints ascending letters up to a mirrored end.
\(A+top-i\) keeps rows at a fixed width.
Time complexity: O(n²).
❓ Frequently Asked Questions
134 is A + E in ASCII (65 + 69). So 134 - i gives the mirrored end letter for the suffix on each row, keeping the total row length constant.A. If the prefix also printed A, rows like BABCD would become BAABCD (extra A). Starting the prefix at i and stopping at B avoids the duplicate.line += ch + " ") or push characters into an array and use arr.join(" ").More JavaScript alphabet patterns
Browse the full list for more variations.
10 people found this page helpful
