JavaScript Palindromic Number Pyramid Pattern

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

What Is This Pattern?

A palindromic number pyramid prints a centered row for each i: ascend 1..i, then descend i-1..1 — each line reads the same both ways.

Remember
Rule: spaces, then 1..i, then i-1..1

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

Unlike Program 55 (column-wise grid), this prints digits directly — row i has 2i - 1 numbers.

How to Solve It

Leading space pairs center the row; ascending then descending halves build the palindrome.

MethodIdeaBest for
Spaces + two halves(rows-i) space pairs, then 1..i, then i-1..1Classic pyramid demos
CompactSame logic with fewer rows (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    repeat (rows - i) times:
        append "  "
    for k from 1 to i:        // ascending
        append k + " "
    for k from i-1 down to 1: // descending
        append k + " "
    print line (trim trailing space)

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= rows; i++)
Leading spacesfor (let s = 0; s < rows - i; s++) line += " ";
Ascending halffor (let k = 1; k <= i; k++) line += k + " ";
Descending halffor (let k = i - 1; k >= 1; k--) line += k + " ";
End the rowconsole.log(line.trimEnd());
Digits on row i2 * i - 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 both halves finish

Append without a newline, then end the row once.

Live Preview

Change the height and the palindromic pyramid updates instantly.

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

Live result rows = 5 · 25 digits
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Worked Walkthrough

Trace three rows when rows = 5 — spaces, ascending, then descending.

Row iSpace pairsAscendDescendPrinted row
141(none)1
321 2 32 11 2 3 2 1
501..54..11 2 3 4 5 4 3 2 1

Descending starts at i - 1 so the peak digit is never printed twice.

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

Space pairs for centering, then ascend 1..i, then descend i-1..1.

JavaScript
const rows = 5;

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

  for (let s = 0; s < rows - i; s++) {
    line += "  ";
  }

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

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

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

How It Works

1. Center with space pairs. Append " " exactly rows - i times so upper rows sit farther right.

2. Ascend to the peak. Loop k = 1..i — for i = 3 that appends 1 2 3.

3. Descend without repeating the peak. Loop k = i-1..1 appends 2 1 → full row 1 2 3 2 1.

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 s = 0; s < rows - i; s++) {
      line += "  ";
    }

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

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

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

How It Works

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

2. Same three-part core. Only the source of rows changes — spaces, ascend, descend match Example 1.

3. Entering 4. Sixteen digits total (sum of odd counts 1+3+5+7), base row 1 2 3 4 3 2 1.

Example 3 — Compact rows = 3

Same spaces + palindrome logic with fewer rows for a quick visual check.

JavaScript
const rows = 3;

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

  for (let s = 0; s < rows - i; s++) {
    line += "  ";
  }

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

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

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

How It Works

1. Shrink the size. Only rows changes — still spaces, ascend, descend.

2. Easier to trace. Nine digits total — useful while learning the peak skip.

3. Same centering rule. Space pairs still equal rows - i.

Edge Cases & Pitfalls

Check these before calling the solution done.

Peak skip

Start descending at i - 1

Starting at i doubles the middle digit (e.g. 1 2 3 3 2 1).

Spaces

Use two spaces per indent step

A single space usually looks off-center next to digit + " ".

rows = 1

Single-row case

Output is just 1 — no leading spaces, descending skipped.

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 row prints O(n) spaces and digits; across n rows the total is quadratic.

Key Takeaways

  • Rule: center with space pairs, then print 1..i..1.
  • Peak once: descend from i - 1, not i.
  • Break the row: call console.log only after both halves finish.
  • Complexity: O(n²) — each row is linear in n.

One line: indent, count up to i, count back down, then log the palindrome.

Frequently Asked Questions

Each row reads the same forward and backward: 1, 1 2 1, 1 2 3 2 1, and so on.
Append (rows - i) pairs of spaces before the numbers on row i — more spaces on upper rows.
Starting at i would print the peak digit twice. Ascending already printed i.
line += appends spaces and digits on the same row. console.log ends the row after both halves finish.
Program 55 fills a 2D array column-wise. Program 56 prints palindromic digits directly with spaces, ascending, and descending loops.
O(n²) for n rows because each row prints O(n) spaces and digits.
Yes — print this pyramid for the top half, then mirror rows from rows-1 down to 1 for the bottom.
One row prints a single 1 with no leading spaces.

Did you know?

Each row prints 1..i..1 with leading space pairs for centering. Row i has 2i-1 digits — total work grows as O(n²) for n rows.

Next: Diagonal Mirror Number Pyramid

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

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