Shrinking Odd Number Sequence in JavaScript

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:
13579
3579
579
79
9Complete JavaScript Program
Outer loop selects the row start (odd values). Inner loop prints odd numbers from current start to end.
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
Pick odd start sequence
i = 1, 3, 5, 7, 9 defines where each row begins.
Outer loop (+2 increment)
for (let i = 1; i <= 9; i += 2) moves through odd row starts only.
Inner loop prints odd sequence
for (let j = i; j <= 9; j += 2) appends odd numbers from current start to 9.
Print row and continue
console.log(line); outputs each row before the next odd start value.
Shrinking odd-number rows
Using step size +2 in both loops keeps parity fixed and naturally produces odd-only sequences.
Variation — Browser (document.write) Version
Print the same pattern directly in an HTML page using document.write:
<!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.writein production UIs
Key Takeaways
The outer loop chooses row starting odd numbers.
The inner loop continues odd values up to the upper bound.
Step size +2 guarantees odd-number output only.
Changing start and limit values creates many sequence variants quickly.
❓ Frequently Asked Questions
i = 1, next start is i = 3.9 with 11 in both loop conditions.document.write.Explore More JavaScript Number Patterns!
Practice custom loop-step patterns to gain stronger control over mathematical sequence outputs.
Loop step size is often an overlooked pattern tool. Changing from +1 to +2 can completely transform output families.
12 people found this page helpful
