Reverse Repeated Number Triangle in JavaScript

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

What You’ll Learn

How to print a reverse repeated number triangle in JavaScript where row values decrease from rows to 1, while the row length increases.

This pattern helps you understand descending outer-loop control with a nested inner loop that repeats the current row value.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5
44
333
2222
11111
1

Complete JavaScript Program

The outer loop moves from rows down to 1. The inner loop repeats i exactly rows - i + 1 times.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set total rows

const rows = 5; sets the starting row value and total line count.

Setup
2

Outer loop (descending value)

for (let i = rows; i >= 1; i--) chooses the value to print on each line, from 5 down to 1.

Row control
3

Inner loop (repeat i)

for (let j = rows; j >= i; j--) runs more times as i gets smaller, and line += i; repeats the same digit.

Number printing
4

Output each completed row

console.log(line); prints one row, then the loops continue until i reaches 1.

Line break
=

Reverse repeated number triangle

Total printed digits are 1+2+…+n = n(n+1)/2, so runtime is O(n²) for n rows.

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

💡 Tips for Enhancement

Try These

  • Accept rows as user input to generate dynamic pattern sizes
  • Add spaces between digits for cleaner output readability
  • Convert this into an ascending variant by reversing outer-loop direction
  • Render pattern inside a <pre> element instead of using document.write
  • Reuse the same loop logic for alphabet or symbol triangles

Avoid

  • Printing j when the pattern requires repeated i
  • Forgetting to reset line inside the outer loop
  • Skipping input checks for invalid row values
  • Using document.write in production apps

Key Takeaways

1

The outer loop controls descending row values from rows to 1.

2

The inner loop repeats the same value, producing rows like 44 and 333.

3

Row length increases as the row value decreases.

4

Nested loops can produce many variants by changing loop direction and print value.

❓ Frequently Asked Questions

When i = 5, inner loop runs once (j = 5), so only one digit is printed.
At i = 1, inner loop runs from j = 5 down to 1, so it prints 1 five times.
Yes. Use the JavaScript example with console.log(line) for clean console output.
O(n²) due to total output size growing with triangular-number count.

Explore More JavaScript Number Patterns!

Practice reverse and forward pattern variations to strengthen your nested-loop skills.

All Number Patterns →
Did you know?

This pattern combines two opposite ideas: the digit value decreases each row, but the row width increases. That contrast is a useful way to practice loop boundaries.

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