Reverse-Forward Number Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Down then up

What You’ll Learn

How to print a triangle where each row first counts down (from i to 2) and then counts up (from 1 to i).

This pattern creates symmetric rows like 32123 and 543212345 using two inner loops.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
212
32123
4321234
543212345
1

Complete JavaScript Program

The first inner loop prints i, i-1, ..., 2. The second prints 1..i. Together they form the reverse-forward row.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";

  for (let j = i; j > 1; j--) {
    line += j;
  }

  for (let j = 1; j <= i; j++) {
    line += j;
  }

  console.log(line);
}

🧠 How It Works

1

Choose the row count

const rows = 5; sets how many rows are printed.

Setup
2

Outer loop builds each row

i goes from 1..rows, increasing the row width each time.

Rows
3

Print descending part (i..2)

for (j = i; j > 1; j--) prints the left portion without including 1.

Down
4

Print ascending part (1..i)

for (j = 1; j <= i; j++) prints the right portion, completing the symmetric row.

Up
=

Row is symmetric

The left half is descending and the right half is ascending, sharing a single 1 in the middle.

2

Variation — Browser (document.write) Version

Print the triangle in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j;
for (i = 1; i <= 5; i++) {
  for (j = i; j > 1; j--)
    document.write(j);
  for (j = 1; j <= i; j++)
    document.write(j);
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Increase rows for a bigger triangle
  • Add spaces between digits (useful for multi-digit values)
  • Center-align by adding leading spaces before each row
  • Replace numbers with letters to build a similar alphabet pattern

Avoid

  • Printing 1 in the first loop (it would duplicate the middle)
  • Forgetting the newline after each row
  • Mixing row and column roles across loops

Key Takeaways

1

Two inner loops build each row: one descending, one ascending.

2

Stopping the first loop at 2 prevents duplicating the center 1.

3

Rows are symmetric and grow with i.

4

This technique generalizes to many mirror-based patterns.

❓ Frequently Asked Questions

The first loop prints 3 then 2, and the second prints 1 2 3, producing 32123.
Yes. Append " " after each digit and trim at the end for clean formatting.
You can include 1 in the descending loop, but then adjust the ascending loop start to avoid duplicating 1 twice.
Yes, but multi-digit values will change spacing. Use separators or fixed-width padding if you want alignment.

Explore More JavaScript Number Patterns!

Mirror and palindrome patterns are excellent practice for controlling loop boundaries.

All Number Patterns →
Did you know?

A tiny change in a loop boundary (like stopping at 2 instead of 1) can completely change whether the center value is duplicated. That’s why off-by-one logic matters a lot in pattern printing.

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.

12 people found this page helpful