Inverted Right-Aligned Triangle Star Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 1 Code Example
spaces + stars per row

What You'll Learn

This program prints an inverted, right-aligned triangle. Stars decrease each row, but the shape stays aligned to the right edge.

For row i (starting at 1), print i - 1 spaces, then print rows - i + 1 stars.

⭐ 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 = 1; i <= rows; i++) {
  let line = "";
  for (let s = 1; s <= i - 1; s++) line += " ";
  for (let j = i; j <= rows; j++) line += "*";
  console.log(line);
}

🧠 How It Works

1

Outer loop

for (let i = 1; i <= rows; i++) prints one line per row index. Each line still spans rows characters: growing padding on the left, shrinking star run on the right.

Outer loop
2

Growing left margin

for (let s = 1; s <= i - 1; s++) line += " "; prints i - 1 spaces (none on the first row).

Alignment
3

Shrinking star run

for (let j = i; j <= rows; j++) line += "*"; prints rows - i + 1 stars. console.log(line) ends the row. Shorthand: " ".repeat(i - 1) + "*".repeat(rows - i + 1).

Stars
=

Inverted right-aligned triangle

O(n²) output for n = rows, O(1) extra space. Same character total as Programs 1–3; layout is the mirror of Program 3.

💡 Tips for Enhancement

Try These

  • Try " ".repeat(i - 1) + "*".repeat(rows - i + 1) as a shorter row builder
  • Compare with Program 3 to see how flipping star counts changes the shape
  • Print spaces between stars using "* ".repeat(rows - i + 1).trimEnd()
  • Try Program 5 (pyramid) after this to practice centered spacing
  • Change the symbol from * to # or @

Avoid

  • Using tabs for alignment (rendering differs across editors)
  • Printing extra spaces at the end of each line (harder to compare outputs)
  • Mixing &nbsp; with normal spaces (output becomes inconsistent)
  • Confusing i - 1 (spaces) with rows - i + 1 (stars)
  • Leaving the output without line breaks (all stars appear on one line)

Key Takeaways

1

Row i prints i - 1 leading spaces.

2

Then it prints rows - i + 1 stars.

3

The triangle stays right-aligned because spaces increase as stars decrease.

4

repeat() can build rows in one expression.

5

Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.

❓ Frequently Asked Questions

For row i, print i - 1 spaces first, then print rows - i + 1 stars.
Yes. Use " ".repeat(i - 1) + "*".repeat(rows - i + 1).
It’s O(n²) for n rows, since printing spaces + stars across all rows is \(\Theta(n^2)\).

Next: Pyramid

Continue to Program 5 to print a centered pyramid using spaces and odd stars.

Program 5 →
Did you know?

Inverted patterns are a quick way to practice thinking in reverse: spaces usually increase while stars decrease.

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