JavaScript Number Pattern (Decreasing then Increasing)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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².
Approach
How to Solve It
One outer row loop, then descending and ascending half-loops. End each row with console.log.
Method
Idea
Best for
Two-half row
Descend i..2, then ascend 1..(n-i+1)
Classic rotating demos
Compact
Same 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
Goal
Pattern
Outer (rows)
for (let i = 1; i <= n; i++)
Descending half
for (let j = i; j > 1; j--) line += j;
Ascending half
for (let k = 1; k <= n + 1 - i; k++) line += k;
End the row
console.log(line);
Digits per row
n (always)
Total digits
n * n
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each digit in both halves
console.log(line)
Ends the current row
After both halves finish
Append without a newline, then end the row once.
Try it
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 resultn = 5 · 25 digits
12345
21234
32123
43212
54321
Trace
Worked Walkthrough
Trace three rows when n = 5 — watch the descending and ascending halves.
Row i
Descend i..2
Ascend 1..(n-i+1)
Printed row
1
(none)
1 2 3 4 5
12345
2
2
1 2 3 4
21234
5
5 4 3 2
1
54321
Left half grows as the right half shrinks — every row still has exactly n digits.
Code
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);
}
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);
}
}
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);
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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².
Remember
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².