Ascending Repeated Inverted Triangle in JavaScript

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

What You’ll Learn

How to print an ascending repeated number inverted triangle in JavaScript where values increase from 1 to rows while row width decreases.

This pattern clearly shows how changing the inner-loop start point controls row length.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
44
5
1

Complete JavaScript Program

Outer loop increases i from 1 to rows. Inner loop runs from i to rows, so output width shrinks each row.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Choose row count

const rows = 5; sets the maximum width for the first row.

Setup
2

Outer loop (ascending value)

for (let i = 1; i <= rows; i++) selects row values in ascending order: 1, 2, 3, 4, 5.

Row control
3

Inner loop (decreasing width)

for (let j = i; j <= rows; j++) prints fewer times as i grows, while line += i; repeats the row value.

Number printing
4

Print each row

console.log(line); outputs one line per iteration until row 5 prints just one digit.

Line break
=

Ascending repeated inverted triangle

Total output count is still triangular: n(n+1)/2, so complexity remains O(n²).

2

Variation — Browser (document.write) Version

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

HTML
<!DOCTYPE html>
<html>
<body>
<script>
const rows = 5;

for (let i = 1; i <= rows; i++) {
  for (let j = i; j <= rows; j++) {
    document.write(i);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Take rows from user input to generate custom sizes
  • Add spaces between digits for improved readability
  • Convert this to a full pyramid by combining left and right halves
  • Render output in a DOM element instead of using document.write
  • Use the same logic with letters for alphabet-pattern practice

Avoid

  • Using the wrong inner-loop start (must begin at i)
  • Mixing i and j outputs unintentionally
  • Skipping input validation for invalid row values
  • Relying on document.write in production code

Key Takeaways

1

The outer loop increases row value from 1 to rows.

2

The inner loop shortens each row by starting from j = i.

3

Row values increase while row widths decrease.

4

Loop boundaries are the key to controlling pattern shape.

❓ Frequently Asked Questions

At i = 1, inner loop runs from 1 to 5, so it prints 1 five times.
When i = 5, inner loop runs once (j = 5), so only one 5 is printed.
Yes. Use the second example with document.write to display output in an HTML page.
O(n²), because total prints are proportional to triangular-number growth.

Explore More JavaScript Number Patterns!

Keep practicing inverted and regular triangle variants to strengthen your loop logic.

All Number Patterns →
Did you know?

This pattern is a nice example where row value increases while width decreases. Opposing loop directions often create visually interesting results.

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