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.
Approach
How to Solve It
Walk i from 1 to n; append the forward segment, then the wrap segment.
Method
Idea
Best for
Two inner loops
Forward i..n; wrap i-1..1
Learning, interviews
prompt n
Same loops; read n at runtime
Interactive practice
Compact n = 3
Same structure; easier to trace by hand
Paper 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
Goal
Pattern
Set width
const n = 5;
Outer loop
for (let i = 1; i <= n; i++)
Forward
for (let j = i; j <= n; j++) line += j;
Wrap
for (let k = i - 1; k >= 1; k--) line += k;
End row
console.log(line);
Digits per row
Always n
vs Program 38
Fixed width + rotate vs shrinking continuous k
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j / line += k
Stays on the same row
Each digit
console.log(line)
Ends the line
After both halves
Build the row with +=, then break once with console.log. Putting console.log inside an inner loop prints one digit per line.
Try it
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 resultn = 5 · digits = 25
12345
23451
34521
45321
54321
Trace
Worked Walkthrough — n = 3
Trace each outer i, the forward segment, the wrap segment, and the printed row.
i
Forward i..n
Wrap i-1..1
Printed row
1
1 2 3
none
123
2
2 3
1
231
3
3
2 1
321
Every row has exactly n digits. When i = 1, the wrap loop never runs.
Code
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);
}
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);
}
}
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);
}