JavaScript Hollow Number Pyramid Pattern

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

What Is This Pattern?

A diagonal mirror number pyramid prints digit i on a left diagonal and again on a right diagonal — spaces fill the gaps, forming an inverse-V that widens downward.

Remember
Rule: digit when i === j (left) or i === k (right)

    1
   2 2
  3   3
 4     4
5       5   ← rows = 5

Unlike Program 56 (full palindrome 1..i..1), only the row digit appears — about 2n - 1 characters per row.

How to Solve It

Left loop counts down from rows; right loop counts up from 2 — each places the digit or a space.

MethodIdeaBest for
Two-half diagonalsj = rows..1 left, k = 2..rows rightClassic inverse-V demos
CompactSame logic with fewer rows (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    for j from rows down to 1:   // left half
        append i if i === j else " "
    for k from 2 to rows:        // right half
        append i if i === k else " "
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= rows; i++)
Left diagonalfor (let j = rows; j >= 1; j--) line += (i === j) ? i : " ";
Right diagonalfor (let k = 2; k <= rows; k++) line += (i === k) ? i : " ";
End the rowconsole.log(line);
Chars per row2 * rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowEach digit or space
console.log(line)Ends the current rowAfter both halves finish

Append without a newline, then end the row once.

Live Preview

Change the height and the inverse-V diagonals update instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 45 chars
    1
   2 2
  3   3
 4     4
5       5

Worked Walkthrough

Trace three rows when rows = 5 — watch where i === j and i === k fire.

Row iLeft at jRight at kPrinted row
1j = 1(none)1
3j = 3k = 33 3
5j = 5k = 55 5

The gap between the two digits grows with i — that is the inverse-V shape.

JavaScript Programs

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

Example 1 — Fixed rows = 5

Append the digit when i === j or i === k; otherwise append a space.

JavaScript
const rows = 5;

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

  for (let j = rows; j >= 1; j--) {
    line += (i === j) ? i : " ";
  }

  for (let k = 2; k <= rows; k++) {
    line += (i === k) ? i : " ";
  }

  console.log(line);
}
Try It Yourself

How It Works

1. Left half places the first digit. Count j down from rows; append i when j matches, else a space.

2. Right half mirrors it. Count k from 2 to rows so the center column is not printed twice.

3. Gap grows with the row. Lower rows place the two digits farther apart — that forms the inverse V.

Example 2 — User Input Rows

Read rows with prompt and validate before drawing.

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

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";

    for (let j = rows; j >= 1; j--) {
      line += (i === j) ? i : " ";
    }

    for (let k = 2; k <= rows; k++) {
      line += (i === k) ? i : " ";
    }

    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Use parseInt and require rows >= 1.

2. Same two-half core. Only the source of rows changes — left and right diagonal loops match Example 1.

3. Entering 4. Each row has 7 characters (2×4 - 1), base row 4 4.

Example 3 — Compact rows = 3

Same diagonal logic with fewer rows for a quick visual check.

JavaScript
const rows = 3;

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

  for (let j = rows; j >= 1; j--) {
    line += (i === j) ? i : " ";
  }

  for (let k = 2; k <= rows; k++) {
    line += (i === k) ? i : " ";
  }

  console.log(line);
}
Try It Yourself

How It Works

1. Shrink the size. Only rows changes — still i === j / i === k.

2. Easier to trace. Five characters per row — useful while learning the mirrored halves.

3. Same center skip. The right loop still starts at 2.

Edge Cases & Pitfalls

Check these before calling the solution done.

Center skip

Start the right loop at k = 2

Starting at 1 duplicates the center column on every row.

Left direction

Count j down from rows

Counting up places the left diagonal on the wrong side of the row.

rows = 1

Single-row case

Output is just 1 — the right loop never runs.

Bad input

Validate parseInt

Check Number.isFinite(rows) and require rows >= 1 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

Each of n rows appends about 2n - 1 characters — total work is quadratic.

Key Takeaways

  • Rule: place digit i on both diagonals; spaces everywhere else.
  • Skip center: start the right loop at k = 2.
  • Break the row: call console.log(line) only after both halves finish.
  • Complexity: O(n²) — n rows × ~2n characters.

One line: scan left then right, print digit i on each diagonal, then log.

Frequently Asked Questions

The left loop places the digit when i === j; the right loop mirrors it when i === k. Row 1 is the exception — only the left digit appears.
Starting at k = 2 skips the center column already handled by the left loop — avoids a double digit in the middle.
Program 56 prints a full palindrome 1..i..1. Program 57 prints only the row digit on two mirror diagonals.
line += appends a digit or space on the same row. console.log ends the row after both halves finish.
Counting down builds the left half from the outer edge inward so the left diagonal lands correctly.
O(n²) for n rows because each row scans about 2n character positions.
Yes — replace the digit with '*' in both diagonal append branches.
One row prints a single 1 — the right loop (k = 2..1) does not run.

Did you know?

Each row prints the row digit on a left diagonal and a right diagonal, with spaces elsewhere. Positions per row = 2×rows-1 — total work O(n²).

Next: Diagonal Mirror Number Diamond

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

Program 58 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