Split Descending + Ascending

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

What You'll Learn

This pattern builds each row by combining two parts: first a descending sequence from the row letter down to B, then an ascending sequence from A up to the row letter. That creates rows like CBABC and EDCBABCDE.

⭐ Pattern Output

Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
1

Node.js / console version

Two loops per row: descending then ascending.

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

Browser version (document.write)

Matches the reference: write characters directly, then 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 > A; j--) document.write(String.fromCharCode(j));
    for (let j = A; j <= i; j++) document.write(String.fromCharCode(j));
    document.write("<br>");
  }
</script>
</body>
</html>

🧠 How It Works

1

Core idea

Outer i runs from A to E. First inner loop prints descending letters from i down to just above A, then the second inner loop prints ascending letters from A to i. This makes rows like DCBABCD.

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

Descending part: i..B (skip A).

2

Ascending part: A..i (includes A).

3

This avoids duplicating A in the center.

4

Time complexity: O(n²).

❓ Frequently Asked Questions

If we included A in the first loop, A would be printed twice. Skipping A in the descending part keeps the center single.
Yes. Add document.write("*") (or string concatenation) between prints, or build an array and join.
Each row prints up to about \(2n\) characters, across \(n\) rows: O(n²).

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 25 →
Did you know?

These patterns are great practice for combining two loops to build a single row string.

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