Rotating Number Pattern in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Wrap-around with two loops

What You’ll Learn

How to print a rotating number pattern where each row starts at the row number and wraps around to complete 1..5.

You’ll use two inner loops: one to print i..5 and one to print i-1..1.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
12345
23451
34521
45321
54321
1

Complete JavaScript Program

First print from i to size. Then append the remaining values by printing from i-1 down to 1.

JavaScript
const size = 5;

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

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

  for (let k = i; k > 1; k--) {
    line += (k - 1);
  }

  console.log(line);
}

🧠 How It Works

1

Set the size

const size = 5; defines the range 1..5.

Setup
2

Outer loop chooses the row start

i runs from 1..5, deciding the first digit of each row.

Rows
3

Print i..size

The loop j = i..size prints the increasing tail (e.g., 3 4 5).

Tail
4

Append i-1..1

The loop counts down and appends the remaining values, completing the wrap-around.

Wrap
=

Row wraps cleanly

Start at 2, go to 5, then wrap back to 1.

2

Variation — Browser (document.write) Version

Print the pattern in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j, k;
for (i = 1; i <= 5; i++) {
  for (j = i; j <= 5; j++)
    document.write(j);
  for (k = i; k > 1; k--)
    document.write(k - 1);
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Make size larger and print multi-digit values with spaces
  • Use arrays and modulo arithmetic to create other rotation patterns
  • Reverse the second loop to change wrap direction
  • Print separators between values for readability

Avoid

  • Forgetting to update both loop bounds when changing size
  • Printing without separators for multi-digit numbers (values can merge)
  • Duplicating digits by using the wrong loop start/end

Key Takeaways

1

Two loops create the wrap-around: i..size then i-1..1.

2

Each row contains all digits 1..size exactly once.

3

Changing the loop boundaries changes the rotation behavior.

4

This is a great exercise for loop thinking and sequence generation.

❓ Frequently Asked Questions

When i = 5, the first loop prints only 5, then the wrap loop appends 4 3 2 1.
Yes. Append " " after each value (and trim the end) for readability.
Yes. You can compute each position using modulo arithmetic, but two loops are often easier to understand for beginners.
It prints a single row with just 1. The wrap loop won’t run.

Explore More JavaScript Number Patterns!

Wrap-around patterns help you practice building sequences in multiple stages.

All Number Patterns →
Did you know?

Many “rotation” problems in programming can be solved either by slicing + concatenation, or by printing in two parts—exactly what we do here with two inner loops.

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