Sequential Decreasing-Width Number Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested loops

What You’ll Learn

How to print consecutive numbers in a triangle where each next row prints one fewer number.

This is a simple and useful practice exercise for nested loops and formatting.

⭐ Pattern Output

For rows = 5, the output looks like this:

Output
1  2  3  4  5\n6  7  8  9\n10 11 12\n13 14\n15
1

Complete JavaScript Program

The inner loop prints fewer values each row, while k keeps increasing.

JavaScript
const rows = 5;
let k = 1;

for (let i = 1; i <= rows; i++) {
  const row = [];
  for (let j = rows; j >= i; j--) {
    row.push(k++);
  }
  console.log(row.join("  "));
}

🧠 How It Works

1

Initialize rows and counter

rows controls the triangle height, and k starts at 1.

Setup
2

Outer loop for each row

i goes from 1 to rows, producing one output line per iteration.

Row control
3

Inner loop decreases the width

The inner loop runs from rows down to i, so each row prints one fewer number than the previous row.

Printing
4

Increment k each time

k++ ensures the numbers remain sequential across all rows.

Counter
=

Sequential triangle

Total prints are \(rows(rows+1)/2\), so runtime grows like O(n²).

2

Variation — Browser Grid Version

Print the same pattern in the browser using fixed-width cells for clean alignment:

HTML
<!DOCTYPE html>
<html>
<head>
  <style>
    .cell { display:inline-block; width:2.2em; text-align:right; }
  </style>
</head>
<body>
<script>
var rows = 5;
var k = 1;
for (var i = 1; i <= rows; i++) {
  for (var j = rows; j >= i; j--) {
    document.write('<span class="cell">' + (k++) + '</span>');
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change rows to print bigger triangles
  • Start from a different number by changing the initial value of k
  • Use padStart to align larger numbers
  • Build each row as a string and then print once for cleaner output formatting

Avoid

  • Hard-coding the number 5 in multiple places (use rows)
  • Forgetting the line break at the end of each row
  • Using document.write in production (fine for demos)

Key Takeaways

1

A decreasing-width triangle can be done by changing the inner loop bounds.

2

A single counter (k) keeps the sequence continuous across rows.

3

Total printed values are \(rows(rows+1)/2\).

4

Use fixed-width formatting (pads/spans) to keep the output neat.

❓ Frequently Asked Questions

Join the numbers with spaces (or use fixed-width spans in HTML) to keep alignment consistent.
Yes—replace k++ with the inner-loop variable (like j or another counter).
Because when i = rows, the inner loop runs only once, so it prints one value.
Print leading spaces before each row based on rows - i, and adjust spacing between numbers accordingly.

Explore More JavaScript Number Patterns!

Try combining sequential counters with different triangle shapes.

All Number Patterns →
Did you know?

The total numbers printed in this triangle is the triangular number \(rows(rows+1)/2\).

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