Inverted Center-Aligned Pyramid Star Pattern in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 1 Code Example
2*i - 1 stars

What You'll Learn

This program prints an inverted center-aligned pyramid. Stars decrease each row, while leading spaces increase to keep the pyramid centered.

Loop from i = rows down to 1. Print rows - i spaces, then print 2*i - 1 stars.

⭐ Pattern Output

When you run the program with rows = 5:

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

Complete JavaScript Program

Fixed rows = 5 version (reverse outer loop):

JavaScript
const rows = 5;

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

🧠 How It Works

1

Reverse outer loop

for (let i = rows; i >= 1; i--) starts at the wide row and shrinks i each time—the opposite direction of Program 5, same inner math.

Outer loop
2

Growing leading spaces

for (let s = 1; s <= rows - i; s++) line += " "; still uses rows - i; as i drops, the margin grows and the shape narrows.

Centering
3

Shrinking odd star block

for (let j = 1; j <= 2 * i - 1; j++) line += "*"; then console.log(line). Star counts step down 9, 7, 5, … when rows = 5.

2*i - 1
=

Inverted pyramid

O(n²) output, O(1) extra space. Same line width as Program 5; the green glyph area scrolls horizontally on phones when rows is large.

💡 Tips for Enhancement

Try These

  • Build rows with " ".repeat(rows - i) + "*".repeat(2 * i - 1)
  • Compare with Program 5 to see how iteration direction flips the shape
  • Try printing spaces between stars using "* ".repeat(2 * i - 1).trimEnd()
  • Make a hollow inverted pyramid by printing only the border stars
  • Change the symbol from * to # or @

Avoid

  • Using tabs for alignment (rendering differs across editors)
  • Using even star counts (breaks centering symmetry)
  • Printing trailing spaces after the stars (can look odd in some outputs)
  • Mixing &nbsp; with normal spaces (output becomes inconsistent)
  • Forgetting the line break after each row

Key Takeaways

1

Use a reverse loop: i = rows .. 1.

2

Row i prints rows - i leading spaces.

3

Then it prints 2*i - 1 stars (odd counts).

4

Spaces increase as stars decrease to keep centering.

5

Time complexity is O(n²) because printing dominates.

❓ Frequently Asked Questions

Because i decreases, 2*i - 1 produces fewer stars each row while rows - i produces more spaces.
Yes. Use " ".repeat(rows - i) + "*".repeat(2 * i - 1).
It’s O(n²) for n rows, since total printed characters are \(\Theta(n^2)\).

Next: Inverted V Hollow

Continue to Program 7 to print an inverted V-shaped hollow star pattern.

Program 7 →
Did you know?

Using the same 2*i - 1 formula, you can flip between a pyramid and an inverted pyramid just by changing the direction of the outer loop.

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