Shrinking Odd Number Sequence in JavaScript

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops (+2 Steps)

What You’ll Learn

How to print a shrinking odd-number sequence in JavaScript using nested loops with a step size of 2.

This pattern is a good exercise for controlling start values and custom loop increments.

⭐ Pattern Output

For odd limit up to 9, the pattern looks like this:

Output
13579
3579
579
79
9
1

Complete JavaScript Program

Outer loop selects the row start (odd values). Inner loop prints odd numbers from current start to end.

JavaScript
for (let i = 1; i <= 9; i += 2) {
  let line = "";
  for (let j = i; j <= 9; j += 2) {
    line += j;
  }
  console.log(line);
}

🧠 How It Works

1

Pick odd start sequence

i = 1, 3, 5, 7, 9 defines where each row begins.

Setup
2

Outer loop (+2 increment)

for (let i = 1; i <= 9; i += 2) moves through odd row starts only.

Row control
3

Inner loop prints odd sequence

for (let j = i; j <= 9; j += 2) appends odd numbers from current start to 9.

Number printing
4

Print row and continue

console.log(line); outputs each row before the next odd start value.

Line break
=

Shrinking odd-number rows

Using step size +2 in both loops keeps parity fixed and naturally produces odd-only sequences.

2

Variation — Browser (document.write) Version

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

HTML
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 1; i <= 9; i += 2) {
  for (let j = i; j <= 9; j += 2) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change upper bound from 9 to any odd limit dynamically
  • Start from 3 to skip the first row and compare outputs
  • Generate even-only version by starting at 2 with step +2
  • Add separators (spaces/commas) between values
  • Render rows into a <pre> element in the DOM

Avoid

  • Using mixed step sizes that break odd-only progression
  • Setting an even upper bound when odd end is expected
  • Skipping validation for invalid limit values
  • Using document.write in production UIs

Key Takeaways

1

The outer loop chooses row starting odd numbers.

2

The inner loop continues odd values up to the upper bound.

3

Step size +2 guarantees odd-number output only.

4

Changing start and limit values creates many sequence variants quickly.

❓ Frequently Asked Questions

Because outer loop increments by 2, so after i = 1, next start is i = 3.
Yes. Replace upper bound 9 with 11 in both loop conditions.
Yes. Example 2 prints it in the page using document.write.
O(n²) in general nested-loop scaling terms.

Explore More JavaScript Number Patterns!

Practice custom loop-step patterns to gain stronger control over mathematical sequence outputs.

All Number Patterns →
Did you know?

Loop step size is often an overlooked pattern tool. Changing from +1 to +2 can completely transform output families.

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