Inverted Right-Angled Triangle Star Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 1 Code Example
n(n+1)/2 stars total

What You'll Learn

This is the inverted version of Program 1. Start with rows stars on the first line and decrease by one star each row until you reach 1 star.

Total stars printed for rows = n is still \(n + (n-1) + \cdots + 1 = \frac{n(n+1)}{2}\).

⭐ Pattern Output

When you run the program with rows = 5:

Output
*****
****
***
**
*
1

Complete JavaScript Program

Fixed rows = 5 version (nested loops):

JavaScript
const rows = 5;

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

🧠 How It Works

1

Countdown outer loop

for (let i = rows; i >= 1; i--) starts at the widest row and decreases i each time, mirroring Program 1’s geometry.

Outer loop
2

Inner loop: i stars

for (let j = 1; j <= i; j++) line += "*"; appends exactly i asterisks to line. Equivalent: line = "*".repeat(i);.

Stars
3

Print the row

console.log(line) sends each completed row to the console with a trailing newline.

Output
=

Inverted triangle

Same total stars as Program 1: n(n+1)/2. O(n²) output, O(1) extra space. Wide first rows scroll horizontally in the green preview on small screens.

💡 Tips for Enhancement

Try These

  • Use "*".repeat(i) to build each row without an inner loop
  • Print the triangle with spaces between stars (e.g., "* ".repeat(i).trimEnd())
  • Compare it with Program 1 to see the mirror effect
  • Try right-aligned versions next (Program 3)

Avoid

  • Counting the outer loop in the wrong direction (you must count down)
  • Forgetting to print a newline per row
  • Mixing tabs and spaces (alignment issues in other patterns)
  • Printing extra spaces at the end of each line (harder to compare outputs)

Key Takeaways

1

Start with rows stars and decrease by 1 each row.

2

Total stars for n rows is \(n(n+1)/2\).

3

repeat() is a clean way to build rows without inner loops.

4

Time complexity is O(n²) because total printed characters grow quadratically.

5

This is the mirror of Program 1.

❓ Frequently Asked Questions

Yes. The only change is the direction of the outer loop: count down from rows to 1.
Print "* ".repeat(i).trimEnd() for each row.
It’s O(n²) for n rows, because total printed characters are proportional to \(n(n+1)/2\).

Next: Right-Aligned Triangle

Continue to Program 3 to print a right-aligned right-angled triangle.

Program 3 →
Did you know?

When printing patterns in Node.js, building each row as a string and printing once per row is usually cleaner than multiple process.stdout.write calls.

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.

9 people found this page helpful