JavaScript Number Pattern (Decreasing then Increasing)

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

What Is This Pattern?

A rotating (mixed) number pattern builds each row from two halves: descending i..2, then ascending 1..(n - i + 1).

Remember
Rule: descend i..2, then ascend 1..(n-i+1)

12345
21234
32123
43212
54321   ← n = 5

Unlike Program 49 (multiplication products), this concatenates digit sequences — every row has exactly n digits; total appends = n².

How to Solve It

One outer row loop, then descending and ascending half-loops. End each row with console.log.

MethodIdeaBest for
Two-half rowDescend i..2, then ascend 1..(n-i+1)Classic rotating demos
CompactSame logic with a smaller n (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from 1 to n:
    line = ""
    for j from i down to 2:
        append j
    for k from 1 to (n - i + 1):
        append k
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= n; i++)
Descending halffor (let j = i; j > 1; j--) line += j;
Ascending halffor (let k = 1; k <= n + 1 - i; k++) line += k;
End the rowconsole.log(line);
Digits per rown (always)
Total digitsn * n

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 size and the rotating pattern 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 n = 5 · 25 digits
12345
21234
32123
43212
54321

Worked Walkthrough

Trace three rows when n = 5 — watch the descending and ascending halves.

Row iDescend i..2Ascend 1..(n-i+1)Printed row
1(none)1 2 3 4 512345
221 2 3 421234
55 4 3 2154321

Left half grows as the right half shrinks — every row still has exactly n digits.

JavaScript Programs

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

Example 1 — Fixed n = 5

Two inner loops: descend i..2, then ascend 1..(n-i+1).

JavaScript
const n = 5;

for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = i; j > 1; j--) {
    line += j;
  }
  for (let k = 1; k <= n + 1 - i; k++) {
    line += k;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop sets the pivot. i is the starting value of the descending half.

2. Descend first. j runs from i down while j > 1 — skipped entirely when i = 1.

3. Ascend to fill the row. k runs to n + 1 - i so every row still has exactly n digits.

Example 2 — User Input Size

Read n with prompt and validate before drawing.

JavaScript
const n = parseInt(prompt("Enter size (n):"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= n; i++) {
    let line = "";
    for (let j = i; j > 1; j--) {
      line += j;
    }
    for (let k = 1; k <= n + 1 - i; k++) {
      line += k;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

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

2. Same two-half core. Only the source of n changes — both loops match Example 1.

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

Example 3 — Compact n = 3

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

JavaScript
const n = 3;

for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = i; j > 1; j--) {
    line += j;
  }
  for (let k = 1; k <= n + 1 - i; k++) {
    line += k;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Shrink the size. Only n changes — still descend then ascend.

2. Easier to trace. Three digits per row instead of five — useful while learning the halves.

3. Same width rule. Each row still has exactly n digits.

Edge Cases & Pitfalls

Check these before calling the solution done.

Ascend bound

Use n + 1 - i (or n - i + 1)

A wrong ascending limit breaks the fixed row width of n digits.

Descend stop

Stop before appending 1 in the first half

Use j > 1 so 1 appears only in the ascending half (avoids a double 1).

n = 1

Single-row case

Output is just 1 — descending is skipped.

Bad input

Validate parseInt

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

There are n rows and n digits per row — total work is n².

Key Takeaways

  • Rule: descend i..2, then ascend 1..(n-i+1).
  • Width: every row has exactly n digits.
  • Break the row: call console.log(line) only after both halves finish.
  • Complexity: O(n²) — n rows × n digits.

One line: for each row, append descending digits then ascending digits until the width is n, then log.

Frequently Asked Questions

Each row appends two parts: descending from i down to 2, then ascending from 1 up to (n - i + 1). Row 2 becomes 2 + 1234 = 21234.
One appends the descending half (j = i down while j > 1). The other appends the ascending half (k = 1..(n-i+1)). Splitting them keeps both halves clear.
When i = 1, the descending loop never runs. Only the ascending loop appends 1..n — a full ascending line.
line += appends on the same row. console.log(line) ends the row after both halves finish.
Program 49 appends i*j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
O(n²) for n rows because each row appends n digits — total appends = n².
Both work for the descending loop and append the same i..2 sequence.
One row prints 1 — the descending loop is skipped and the ascending loop appends only 1.

Did you know?

Each row combines two sequences: descending i..2, then ascending 1..(n-i+1). Row 2 prints 21234; row 5 prints 54321. Each row has n digits — total appends = n².

Next: Alternating Ascending/Descending Triangle

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

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