Shifted Rows (ABC.. / BAB..)

Beginner
⏱️ 10 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
134-i mirror

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

Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
1

Node.js / console version

First loop prints i..B, second loop prints A..(134-i).

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 > A; j--) line += String.fromCharCode(j);
  for (let k = A; k <= (A + top) - i; k++) line += String.fromCharCode(k);
  console.log(line);
}
2

Browser version (document.write)

Matches the reference logic using ASCII codes (with the clearer A + top - i form).

HTML
<!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

1

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.

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

Prefix loop prints descending letters from the row start.

2

Suffix loop prints ascending letters up to a mirrored end.

3

\(A+top-i\) keeps rows at a fixed width.

4

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.
The suffix loop always starts from 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.
Append a space after each printed character (for example line += ch + " ") or push characters into an array and use arr.join(" ").

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 31 →

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