Mirrored Diagonal Numbers Pattern

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Diagonals + spacing

What You’ll Learn

How to print a pattern where each row prints its index on the left diagonal and again on the right mirrored diagonal.

This is a good exercise for understanding coordinates \((row, column)\) and spacing.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
1       1
 2     2
  3   3
   4 4
    5
1

Complete JavaScript Program

We scan across a row and print the row number when the column matches a diagonal position. Otherwise we print spaces. (Use a monospace font for perfect alignment.)

JavaScript
const n = 5;
const width = 2 * n - 1;

for (let i = 1; i <= n; i++) {
  let line = "";
  for (let col = 1; col <= width; col++) {
    const leftDiag = col === i;
    const rightDiag = col === width - i + 1;
    line += leftDiag || rightDiag ? String(i) : " ";
  }
  console.log(line.trimEnd());
}

🧠 How It Works

1

Set n and width

We use width = 2*n - 1 so there is room for two diagonals.

Setup
2

Row loop (i)

Each row prints the digit i at two positions.

Rows
3

Left diagonal

At column col === i, we print the left diagonal digit.

Diagonal
4

Right diagonal

At column col === width - i + 1, we print the mirrored digit.

Mirror
=

Two diagonals

Everything else is spaces, so alignment depends on fixed-width spacing.

2

Variation — Browser (document.write) Version

In HTML, use &nbsp; for spaces so the diagonals don’t collapse:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var n = 5;
var width = 2 * n - 1;
for (var i = 1; i <= n; i++) {
  for (var col = 1; col <= width; col++) {
    if (col === i || col === width - i + 1) document.write(i);
    else document.write("&nbsp;");
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Print * instead of numbers to create an X pattern
  • Increase n for a larger mirrored diagonal grid
  • Use padStart to align multi-digit row numbers
  • Print both diagonals for a full X by looping rows up to width

Avoid

  • Using normal spaces in HTML (they collapse)
  • Using proportional fonts for console output comparisons
  • Forgetting to trim trailing spaces if needed

Key Takeaways

1

Main diagonal condition is col === i.

2

Mirrored diagonal condition is col === width - i + 1.

3

Use &nbsp; in HTML to preserve spacing.

4

This is a coordinate-rule pattern: print special values at specific columns.

❓ Frequently Asked Questions

On the last row, the two diagonal positions meet at the same column, so only one digit is printed.
Loop rows from 1 to width, and use conditions col === row and col === width - row + 1.
Use fixed-width formatting (pad numbers) so multi-digit values don’t break alignment.
Wrap the output in a <pre> element or use inline-block cells with fixed width for perfect control.

Explore More JavaScript Number Patterns!

Diagonal rules are powerful. Next, try mixing diagonals with borders or center columns to create complex shapes.

All Number Patterns →
Did you know?

The two diagonal formulas col === row and col === width - row + 1 are used in many classic “X” and “cross” patterns.

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