Centered Number Diamond (1..2i-1) in JavaScript

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Symmetry (top + bottom)

What You’ll Learn

How to print a centered number diamond in JavaScript. The top half grows by odd counts (1, 3, 5…) and the bottom half mirrors it back down.

You’ll practice nested loops, indentation for centering, and building symmetric output.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete JavaScript Program

We build each row as a string: first indentation spaces, then digits from 1 to 2*i - 1. We print the top half and then the mirrored bottom half.

JavaScript
const size = 5;

// Top half (including middle)
for (let i = 1; i <= size; i++) {
  let line = " ".repeat(size - i);
  for (let k = 1; k <= 2 * i - 1; k++) {
    line += k;
  }
  console.log(line);
}

// Bottom half
for (let i = size - 1; i >= 1; i--) {
  let line = " ".repeat(size - i);
  for (let k = 1; k <= 2 * i - 1; k++) {
    line += k;
  }
  console.log(line);
}

🧠 How It Works

1

Choose size

const size = 5; controls the diamond height (top half has 5 rows).

Setup
2

Top half loop (i = 1..size)

Each row i prints 2*i - 1 digits and is centered using size - i spaces.

Grow
3

Bottom half loop (i = size-1..1)

We repeat the same logic in reverse to mirror the top half and complete the diamond.

Mirror
4

Digits per row

The inner loop prints digits 1..(2*i - 1), which produces the odd-length rows: 1, 3, 5…

Odd counts
=

Centered number diamond

Top + bottom symmetry produces the diamond shape with a single rule for spaces and digit count.

2

Variation — Browser (document.write) Version

This variation uses document.write and &nbsp; so the pattern aligns correctly in HTML output.

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j, k;
var size = 5;

// Top half (including middle)
for (i = 1; i <= size; i++) {
  for (j = i; j < size; j++)
    document.write("&nbsp;&nbsp;");
  for (k = 1; k < i * 2; k++)
    document.write(k);
  document.write("<br>");
}

// Bottom half
for (i = size - 1; i >= 1; i--) {
  for (j = size; j > i; j--)
    document.write("&nbsp;&nbsp;");
  for (k = 1; k < i * 2; k++)
    document.write(k);
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change size to print a taller (or smaller) diamond
  • Add spaces between digits (print k + " ") for readability
  • Print in reverse order to create 9..1 diamonds
  • Replace digits with characters to create an alphabet diamond

Avoid

  • Using normal spaces in HTML (they collapse); use &nbsp;
  • Forgetting the mirrored bottom half (you’ll only get a pyramid)
  • Mixing up 2*i - 1 with 2*i (row widths will be off by one)

Key Takeaways

1

A centered diamond is a pyramid + a mirrored pyramid.

2

Indentation is size - i spaces for each row.

3

Each row prints 2*i - 1 digits to create odd widths.

4

Use &nbsp; when printing aligned patterns in the browser.

❓ Frequently Asked Questions

A centered diamond expands equally on both sides. Printing 2*i - 1 digits ensures there’s a single center digit and symmetric growth.
Yes—print line += k + " "; and adjust indentation (use two spaces per step) to keep alignment.
Change size. The widest row will have 2*size - 1 digits.
Yes—print each number using a fixed width (e.g., String(k).padStart(2)) and use matching indentation so columns align.

Explore More JavaScript Number Patterns!

Diamonds are a great milestone. Next, try combining indentation with multiple sequences to create even richer patterns.

All Number Patterns →
Did you know?

Diamonds and pyramids often share the same core formula: leading spaces + an odd-length row. Once you master 2*i - 1, you can build many symmetric patterns quickly.

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