0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Inverted Pyramid Pattern

You can print an inverted pyramid in JavaScript using nested loops like for (let i = n; i >= 1; i--) { console.log(' '.repeat(n - i) + '* '.repeat(i)); } where n is the number of rows.
📋

Examples

Input3
Output* * * * * *
Input5
Output* * * * * * * * * * * * * * *
Input1
Output*
🧠

How to Think About It

To print an inverted pyramid, start from the maximum number of stars in the first row and decrease the stars by one each row. Add spaces before the stars to keep the pyramid shape aligned to the right. Use one loop to handle rows and nested loops or string repeat to print spaces and stars.
📐

Algorithm

1
Get the number of rows (n) as input.
2
For each row from n down to 1:
3
Print spaces equal to (n - current row number) to shift stars right.
4
Print stars followed by a space equal to the current row number.
5
Move to the next line after printing each row.
💻

Code

javascript
function printInvertedPyramid(n) {
  for (let i = n; i >= 1; i--) {
    const spaces = ' '.repeat(n - i);
    const stars = '* '.repeat(i);
    console.log(spaces + stars);
  }
}

printInvertedPyramid(5);
Output
* * * * * * * * * * * * * * *
🔍

Dry Run

Let's trace printing an inverted pyramid with 3 rows through the code.

1

Start outer loop with i = 3

spaces = ' '.repeat(0) = '', stars = '* '.repeat(3) = '* * * ', print '* * * '

2

Next iteration i = 2

spaces = ' '.repeat(1) = ' ', stars = '* '.repeat(2) = '* * ', print ' * * '

3

Next iteration i = 1

spaces = ' '.repeat(2) = ' ', stars = '* '.repeat(1) = '* ', print ' * '

ispacesstarsprinted line
3'''* * * '* * *
2' ''* * ' * *
1' ''* ' *
💡

Why This Works

Step 1: Outer loop controls rows

The loop runs from the total number of rows down to 1, reducing stars each time to create the inverted shape.

Step 2: Spaces align the pyramid

Spaces before stars increase each row to push the stars right, maintaining the pyramid shape.

Step 3: Stars print the pattern

Stars are repeated according to the current row number, decreasing each row to form the inverted pyramid.

🔄

Alternative Approaches

Using nested for loops
javascript
function printInvertedPyramidNested(n) {
  for (let i = n; i >= 1; i--) {
    let line = '';
    for (let s = 0; s < n - i; s++) {
      line += ' ';
    }
    for (let star = 0; star < i; star++) {
      line += '* ';
    }
    console.log(line);
  }
}

printInvertedPyramidNested(5);
This approach uses explicit loops instead of string repeat, which may be easier to understand but less concise.
Using recursion
javascript
function printInvertedPyramidRec(n, current = n) {
  if (current === 0) return;
  console.log(' '.repeat(n - current) + '* '.repeat(current));
  printInvertedPyramidRec(n, current - 1);
}

printInvertedPyramidRec(5);
Recursion provides a different style but can be less efficient and harder to debug for beginners.

Complexity: O(n^2) time, O(n) space

Time Complexity

The outer loop runs n times and the inner string repeat operations take up to n steps, resulting in O(n^2) time.

Space Complexity

Extra space is used for strings of spaces and stars each line, up to O(n), but no large additional data structures.

Which Approach is Fastest?

Using string repeat is generally faster and cleaner than nested loops or recursion for this pattern.

ApproachTimeSpaceBest For
String repeatO(n^2)O(n)Clean and efficient for pattern printing
Nested loopsO(n^2)O(n)Clear logic, easier for beginners
RecursionO(n^2)O(n)Alternative style, less common for simple patterns
💡
Use ' '.repeat() and '* '.repeat() to easily create spaces and stars for each line.
⚠️
Beginners often forget to add spaces before stars, causing the pyramid to be left-aligned instead of inverted and centered.