Odd-Length Descending Number Rows in JavaScript

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

What You’ll Learn

How to print an odd-length descending row pattern in JavaScript where each row starts from 1 and ends at a shrinking odd limit.

This pattern is great for learning custom loop steps like i -= 2.

⭐ Pattern Output

For starting value i = 7, the pattern looks like this:

Output
1234567
12345
123
1
1

Complete JavaScript Program

Outer loop reduces row length by 2 each time. Inner loop prints numbers from 1 up to current i.

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

🧠 How It Works

1

Start at an odd width

i = 7 makes the first row 1234567.

Setup
2

Outer loop (step by -2)

for (let i = 7; i >= 1; i -= 2) keeps row lengths odd: 7, 5, 3, 1.

Row control
3

Inner loop prints 1..i

for (let j = 1; j <= i; j++) appends each number in ascending order to build one row.

Number printing
4

Print and continue

console.log(line); outputs the current row, then next odd width is processed.

Line break
=

Odd-length descending rows

Custom loop steps like i -= 2 are useful for creating selective row sizes and skipping values.

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 = 7; i >= 1; i -= 2) {
  for (let j = 1; j <= i; j++) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Take starting size from user input (for example 9 or 11)
  • Switch to i -= 1 to print every row length
  • Use even starts to generate 8, 6, 4, 2 style output
  • Add spaces between digits for readability
  • Render pattern in a <pre> block with DOM APIs

Avoid

  • Using wrong outer step when you need odd-only widths
  • Changing inner-loop bounds that break 1..i sequence
  • Skipping input validation for non-positive start values
  • Using document.write for production UIs

Key Takeaways

1

The outer loop controls row lengths and skips by 2 each step.

2

The inner loop always prints ascending numbers from 1 to current i.

3

Odd-length rows create a distinct shrinking sequence: 7,5,3,1.

4

Loop step size is a powerful tool for selective pattern generation.

❓ Frequently Asked Questions

Because the outer loop uses i -= 2, it only visits odd lengths.
Change the outer loop to for (let i = 9; i >= 1; i -= 2).
Yes. The second example demonstrates browser output using document.write.
O(n²) in general scaling terms, due to nested-loop output growth.

Explore More JavaScript Number Patterns!

Practice step-based loop patterns to gain stronger control over row selection and output shape.

All Number Patterns →
Did you know?

Using a custom loop step like i -= 2 is an easy way to filter rows by parity without adding extra if checks.

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