Continuous Number Triangle in JavaScript

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

What You’ll Learn

How to print a continuous number triangle where values keep increasing across rows instead of restarting each row.

This pattern is a classic exercise in using a shared state variable across nested loops.

⭐ Pattern Output

For rows = 4, the pattern looks like this:

Output
1
2 3
4 5 6
7 8 9 10
1

Complete JavaScript Program

Outer loop controls row size. Inner loop prints values and increments a running counter.

JavaScript
const rows = 4;
let k = 1;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += k + (j < i ? " " : "");
    k++;
  }
  console.log(line);
}

🧠 How It Works

1

Initialize counter

let k = 1; keeps track of next number to print.

Setup
2

Outer loop sets row length

for (let i = 1; i <= rows; i++) creates rows of length 1, 2, 3, and 4.

Row control
3

Inner loop prints and increments

Each inner iteration appends k and then increments it, so numbering continues across rows.

Counter progression
4

Output each row

console.log(line); prints one completed row before moving on.

Line break
=

Continuous numbering triangle

A shared counter is the key difference from row-reset patterns.

2

Variation — Browser (document.write) Version

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

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

💡 Tips for Enhancement

Try These

  • Increase row count to grow sequence depth
  • Align columns with padding for neat triangular display
  • Start counter from a custom number (e.g., 100)
  • Print the same logic in reverse row order
  • Render to a DOM container instead of document.write

Avoid

  • Resetting the counter inside the outer loop unintentionally
  • Mixing row and counter variables in wrong scope
  • Skipping validation for invalid row values
  • Using document.write in modern production apps

Key Takeaways

1

The outer loop controls how many numbers each row has.

2

A single running counter keeps numbering continuous.

3

Inner loop prints and increments in one pass.

4

This is a base pattern for Floyd-like number triangles.

❓ Frequently Asked Questions

Because previous rows already consumed 1, 2, and 3; the next counter value is 4.
Set rows = 5 in both examples.
Yes. Example 2 uses document.write for browser rendering.
O(n²) due to triangular output size.

Explore More JavaScript Number Patterns!

Master running-counter patterns to solve a wide range of progressive sequence problems.

All Number Patterns →
Did you know?

Continuous number triangles are foundational for many matrix-filling and indexing algorithms.

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