JavaScript Number Pattern (Rotating Digits)

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

What Is This Pattern?

A rotating number pattern prints one full pass of 1..n on every row — but each row starts at i, continues to n, then wraps down i-1..1.

Remember
Rule: for i = 1..n
      print i..n, then i-1..1

12345
23451
34521
45321
54321     ← n = 5

After Program 38’s shrinking sequence, this one keeps a fixed width and rotates the starting digit each row.

How to Solve It

Walk i from 1 to n; append the forward segment, then the wrap segment.

MethodIdeaBest for
Two inner loopsForward i..n; wrap i-1..1Learning, interviews
prompt nSame loops; read n at runtimeInteractive practice
Compact n = 3Same structure; easier to trace by handPaper walkthrough

Pseudocode

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

Cheat sheet

GoalPattern
Set widthconst n = 5;
Outer loopfor (let i = 1; i <= n; i++)
Forwardfor (let j = i; j <= n; j++) line += j;
Wrapfor (let k = i - 1; k >= 1; k--) line += k;
End rowconsole.log(line);
Digits per rowAlways n
vs Program 38Fixed width + rotate vs shrinking continuous k

Printing Numbers vs Starting a New Line

APIEffectUse for
line += j / line += kStays on the same rowEach digit
console.log(line)Ends the lineAfter both halves

Build the row with +=, then break once with console.log. Putting console.log inside an inner loop prints one digit per line.

Live Preview

Change n and the rotating pattern updates instantly.

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

Live result n = 5 · digits = 25
12345
23451
34521
45321
54321

Worked Walkthrough — n = 3

Trace each outer i, the forward segment, the wrap segment, and the printed row.

iForward i..nWrap i-1..1Printed row
11 2 3none123
22 31231
332 1321

Every row has exactly n digits. When i = 1, the wrap loop never runs.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and compact n = 3. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 5

Hard-coded width — forward i..n, then wrap i-1..1.

JavaScript
const n = 5;

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

How It Works

1. Outer loop. i is the starting digit of that row.

2. Forward. Append j from i up to n.

3. Wrap. Append k from i - 1 down to 1 (empty when i = 1); then console.log(line).

Example 2 — prompt Input

Read n at runtime with prompt and parseInt.

JavaScript
const n = parseInt(prompt("Enter 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 <= n; j++) {
      line += j;
    }
    for (let k = i - 1; k >= 1; k--) {
      line += k;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Parse the answer; reject non-positive or non-numeric input.

2. Same rotation. Both inner loops still use i and n — only the width changes.

3. Fixed width. Every row still prints exactly n digits.

Example 3 — Compact n = 3

Same two-loop structure with a smaller width — easy to trace on paper.

JavaScript
const n = 3;

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

How It Works

1. Same structure. Outer and both inner loops match Example 1.

2. Only n changes. Total digits = 3 × 3 = 9 = n².

3. Trace first. Walk i = 1, 2, 3 on paper before scaling to 5 or more.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrap direction

Ascending wrap

Using k = 1..i-1 instead of descending yields a pure cycle (23451 style) — a different pattern.

double i

Repeated start

If the wrap loop includes i, you print i twice. Start wrap at i - 1.

log inside

Vertical digits

If console.log is inside either inner loop, each digit lands on its own line.

n = 1

Single digit

Output is just 1 — the wrap loop never runs.

multi-digit

Values above 9

When n > 9, cells become two characters and the visual rotation breaks. Keep demos at n ≤ 9.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

Each of n rows appends n digits → total n² → O(n²).

Key Takeaways

  • Rule: print i..n, then i-1..1, on each row.
  • Fixed width: every row has exactly n digits.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: total digits = n² → O(n²).

One line: start at i, climb to n, then wrap down to 1.

Frequently Asked Questions

For n = 5: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back down to 1.
The first loop appends i..n (forward). The second appends i-1 down to 1 (wrap). Together they always produce n digits.
After appending 2 3 4 5, the wrap loop runs k from i-1 down to 1 — for i = 2 that appends 1.
Exactly n digits every time — (n - i + 1) forward plus (i - 1) wrap equals n.
Program 38 prints a shrinking continuous sequence. Program 39 rotates: i..n then i-1..1 on every row.
Replace 5 with n in the outer and forward bounds — see Example 2.
O(n²) for n rows because each row appends n digits.
Yes. Instead of descending wrap k from i-1 to 1, append ascending 1..i-1.
Use parseInt with Number.isFinite so bad input does not produce NaN.

Did you know?

Each row starts at i, appends i..n, then wraps with i-1..1. Row i always appends exactly n digits — total digits = n².

Next: Alternating 1/0 Triangle

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

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