JavaScript Palindrome Number Pattern (Increasing-Decreasing)

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

What Is This Pattern?

An increasing-decreasing number pyramid prints a palindromic row for each i: ascend from i to 2i - 1, then descend back to i.

Remember
Rule: ascend i..(2i-1), then descend (skip peak)

1
232
34543
4567654
567898765   ← rows = 5

Unlike Program 51 (one continuous counter), each row resets m = i — total digits = n².

How to Solve It

Set m = i, append ascending, step back with m -= 2, then append descending.

MethodIdeaBest for
Two-half rowAscend i times, m -= 2, descend i - 1 timesClassic palindrome demos
CompactSame logic with fewer rows (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from 1 to rows:
    m = i
    line = ""
    repeat i times:     // ascending
        append m; m += 1
    m -= 2              // skip peak
    repeat i-1 times:   // descending
        append m; m -= 1
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= rows; i++)
Start of rowlet m = i;
Ascending halffor (let j = 0; j < i; j++) { line += m; m++; }
Skip peakm -= 2;
Descending halffor (let k = 0; k < i - 1; k++) { line += m; m--; }
End the rowconsole.log(line);
Digits on row i2 * i - 1
Total digitsrows * rows

Printing Numbers vs Starting a New Line

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

Append without a newline, then end the row once.

Live Preview

Change the row count and the palindromic pyramid updates instantly.

Whole numbers from 1 to 9 (single digits stay neat). Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 25 digits
1
232
34543
4567654
567898765

Worked Walkthrough

Trace three rows when rows = 5 — watch the ascending half, peak skip, and descending half.

Row iAscendAfter m -= 2DescendPrinted row
11(skip)(none)1
22 3m = 22232
33 4 5m = 44 334543

After ascending, m sits one past the peak — m -= 2 lands on the first descending digit.

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

Ascend with m++, skip the peak with m -= 2, then descend with m--.

JavaScript
const rows = 5;

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

  for (let j = 0; j < i; j++) {
    line += m;
    m++;
  }

  m -= 2;

  for (let k = 0; k < i - 1; k++) {
    line += m;
    m--;
  }

  console.log(line);
}
Try It Yourself

How It Works

1. Start each row at i. m = i so row 3 begins at 3, row 5 at 5.

2. Ascend to the peak. Loop i times with m++ — for i = 3 that appends 345.

3. Skip the peak, then descend. m -= 2 avoids a double middle digit; the second loop appends 43 → 34543.

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 m = i;
    let line = "";

    for (let j = 0; j < i; j++) {
      line += m;
      m++;
    }

    m -= 2;

    for (let k = 0; k < i - 1; k++) {
      line += m;
      m--;
    }

    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 — ascend, peak skip, descend match Example 1.

3. Entering 4. Sixteen digits total (4²), last row 4567654.

Example 3 — Compact rows = 3

Same ascend / peak-skip / descend logic with fewer rows for a quick visual check.

JavaScript
const rows = 3;

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

  for (let j = 0; j < i; j++) {
    line += m;
    m++;
  }

  m -= 2;

  for (let k = 0; k < i - 1; k++) {
    line += m;
    m--;
  }

  console.log(line);
}
Try It Yourself

How It Works

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

2. Easier to trace. Nine digits total — useful while learning m -= 2.

3. Same width rule. Row i still has exactly 2i - 1 digits.

Edge Cases & Pitfalls

Check these before calling the solution done.

Peak skip

Always use m -= 2 before descending

Skipping this step repeats the middle digit (e.g. 345543 instead of 34543).

Descend count

Run the descending loop i - 1 times

Ascending already printed the peak — the mirror half is one shorter.

rows = 1

Single-row case

Output is just 1 — the descending loop is 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

Row i prints 2i - 1 digits; summing odd numbers gives n² total work.

Key Takeaways

  • Rule: ascend i..(2i-1), then descend back to i.
  • Peak skip: m -= 2 prevents printing the middle digit twice.
  • Break the row: call console.log(line) only after both halves finish.
  • Complexity: O(n²) — odd digits per row sum to n².

One line: for each row, ascend from i, skip the peak with m -= 2, then descend and log.

Frequently Asked Questions

Row 3 starts at 3, appends up to 5 (345), then after m -= 2 appends back down to 3 (43) — producing 34543.
After the increasing loop, m is one past the peak. Subtracting 2 lands on the value just before the peak so the decreasing loop does not repeat the middle digit.
Each row counts up from i to 2i-1, then back down to i — the same sequence forwards and backwards.
line += appends on the same row. console.log(line) ends the row after both halves finish.
Program 51 uses one continuous counter across rows. Program 52 resets m = i each row and builds a palindromic line.
O(n²) for n rows because row i logs 2i-1 digits and 1+3+…+(2n-1) = n².
Yes — use line += m + " " in both loops and trim before console.log if needed.
One row prints 1 — the decreasing loop never runs when i = 1.

Did you know?

Each row is palindromic: append i..(2i-1) ascending, then back down after m -= 2 skips the peak. Row 3 logs 34543 — total digits = 1+3+5+…+(2n-1) = n².

Next: Mirror Diagonal Number Pattern

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

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