JavaScript Number Diamond Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A number diamond prints ascending digits in odd-length rows that grow to a peak, then mirror back down — centered with leading spaces.

Remember
Rule: top 1..rows, bottom rows-1..1; each row appends 1..(2*i-1)

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1     ← rows = 5

Unlike Program 43 (right-aligned triangle), this pattern is symmetric — total lines for n levels is 2n - 1. Cap at 5 so digits stay single (1–9).

How to Solve It

Build the top half, then mirror it downward — spaces first, then digits concatenated with no gaps. End each row with console.log.

MethodIdeaBest for
Two-half diamondTop 1..rows, bottom rows-1..1Classic diamond demos
CompactSame logic with a smaller rows (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from 1 to rows:
    line = spaces of length (rows - i)
    for k from 1 to (2*i - 1):
        append k
    print line
for i from rows-1 down to 1:
    line = spaces of length (rows - i)
    for k from 1 to (2*i - 1):
        append k
    print line

Cheat sheet

GoalPattern
Top halffor (let i = 1; i <= rows; i++)
Bottom halffor (let i = rows - 1; i >= 1; i--)
Leading spaceslet line = " ".repeat(rows - i);
Odd-length digitsfor (let k = 1; k < 2 * i; k++) line += k;
End the rowconsole.log(line);
Total lines2 * rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowSpaces and each digit
console.log(line)Ends the current rowAfter spaces and digits finish

Append without a newline, then end the row once.

Live Preview

Change the level count and the number diamond updates instantly.

Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 9 lines
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Worked Walkthrough

Trace spaces and digits when rows = 3 — up, then back down.

HalfiSpaces / digitsPrinted row
Top12 / 11
Top21 / 123123
Top30 / 1234512345
Bottom21 / 123123
Bottom12 / 11

Total lines = 2 * rows - 1. Digit count on row i = 2 * i - 1.

JavaScript Programs

Three complete programs: fixed rows = 5, prompt input, and a compact 3-level demo. Use View Output for samples, or Try It Yourself to edit and run live.

Example 1 — Fixed rows = 5

Top half grows to 123456789; bottom half mirrors back to 1.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = " ".repeat(rows - i);
  for (let k = 1; k < 2 * i; k++) {
    line += k;
  }
  console.log(line);
}

for (let i = rows - 1; i >= 1; i--) {
  let line = " ".repeat(rows - i);
  for (let k = 1; k < 2 * i; k++) {
    line += k;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Grow the top. Outer loop i = 1..rows — each row gets rows - i spaces, then digits 1..(2*i-1).

2. Mirror the bottom. Second loop runs i = rows-1..1 with the same space and digit rules.

3. Concatenate digits. line += k (no space) keeps the row as a solid digit string like 12345.

Example 2 — User Input Levels

Read the level count with prompt and validate before drawing.

JavaScript
const rows = parseInt(prompt("Enter number of levels (1-5):"), 10);

if (!Number.isFinite(rows) || rows < 1 || rows > 5) {
  console.log("Please enter an integer from 1 to 5.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = " ".repeat(rows - i);
    for (let k = 1; k < 2 * i; k++) {
      line += k;
    }
    console.log(line);
  }
  for (let i = rows - 1; i >= 1; i--) {
    let line = " ".repeat(rows - i);
    for (let k = 1; k < 2 * i; k++) {
      line += k;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Cap at 5 so the peak digit stays within 1–9.

2. Same diamond core. Only the source of rows changes — both halves match Example 1.

3. Entering 3. Five lines total (2*3 - 1), peaking at 12345.

Example 3 — Compact rows = 3

Same two-half logic with a smaller height for a quick visual check.

JavaScript
const rows = 3;

for (let i = 1; i <= rows; i++) {
  let line = " ".repeat(rows - i);
  for (let k = 1; k < 2 * i; k++) {
    line += k;
  }
  console.log(line);
}

for (let i = rows - 1; i >= 1; i--) {
  let line = " ".repeat(rows - i);
  for (let k = 1; k < 2 * i; k++) {
    line += k;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Shrink the height. Only rows changes — still two outer loops and odd-length digit rows.

2. Easier to trace. Five lines instead of nine — useful while learning the space formula.

3. Same peak rule. Middle row still has 2*rows - 1 digits (12345 when rows = 3).

Edge Cases & Pitfalls

Check these before calling the solution done.

Odd width

Use k < 2 * i (or k <= 2*i - 1)

Looping only to i makes rows 1, 12, 123… instead of odd lengths.

rows > 5

Single-digit peak breaks past 5

line += k prints 10, 11… and columns break. Cap at 5, or use fixed-width formatting.

rows = 1

Single-level case

Output is just 1 — the bottom loop never runs.

Bad input

Validate parseInt

Check Number.isFinite(rows) and require 1 <= rows <= 5 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(n) per row string
User input (Example 2)O(n²)O(n) per row string

There are 2n - 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.

Key Takeaways

  • Rule: grow i to rows, then shrink; each row appends 1..(2*i-1).
  • Center: leading spaces of length rows - i on both halves.
  • Break the row: call console.log(line) only after spaces and digits finish.
  • Complexity: O(n²) time — 2n-1 rows with up to O(n) digits each.

One line: pad left, append odd-length digits up to the peak, then mirror the same rows downward.

Frequently Asked Questions

A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row appends 2*i-1 digits (odd length): 1, 3, 5, 7, 9, … using for (k = 1; k < 2 * i; k++).
Leading spaces via " ".repeat(rows - i) before the digit loop on both halves.
The first builds the top half (i = 1..rows). The second mirrors back down (i = rows-1..1) to complete the diamond.
line += appends on the same row. console.log(line) ends the row after spaces and digits finish.
Program 43 is a right-aligned triangle with spaced columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
O(n²) for n levels because each level appends O(n) digits and there are O(n) rows overall.
Only one row prints — a single centered 1. The bottom loop never runs.

Did you know?

Top half runs i = 1..rows; bottom half mirrors i = rows-1..1. Each row appends 2*i-1 digits (1 to 2*i-1) with leading spaces. Total lines = 2*rows - 1.

Next: X Pattern with Stars and Zeros

Continue with the next pattern in the JavaScript number-pattern series.

Program 45 tutorial →

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