Horizontally Inverted Pyramid

  • Define a function that takes the number of rows as input.
  • Inside the function, we use nested loops to iterate through each row and each position within the row.
  • The first loop will iterate through each row.
  • Second loop will print spaces before the stars based on the current row number
    • If it’s the first row or the first or last position in the row, we print a star.
    • Otherwise, we print a space.
  • Finally, we print each row to create the inverted hollow star pyramid.

Example: Here we are printing a Inverted hollow start pyramid.

Javascript
// Function to print an inverted hollow star pyramid
function printInvertedHollowStarPyramid(rows) {

    // Loop through each row
    for (let i = 1; i <= rows; i++) {
    
       // Initialize a string for the current row
        let rowString = ''; 
        
        // Print spaces before the stars
        for (let j = 1; j < i; j++) {
        
          // Add a space to the row string
            rowString += " "; 
        }
        
        // Print stars or spaces for each row
        for (let j = 1; j <= (rows * 2 - (2 * i - 1)); j++) {
        
            // Check conditions to print stars or spaces
            if (i === 1 || j === 1 || j === (rows * 2 - (2 * i - 1))) {
            
               // Add a star to the row string
                rowString += "*"; 
            } else {
            
               // Add a space to the row string
                rowString += " "; 
            }
        }
        
        // Print the row
        console.log(rowString);
    }
}

// Example usage:
const rows = 5;
printInvertedHollowStarPyramid(rows);

Output
*********
 *     *
  *   *
   * *
    *

JavaScript Program to Print Inverted Hollow Star Pyramid

In this article, we will write a Program to Print an Inverted Hollow Star pyramid.

Possible Inverted Hollow Star Pyramid Patterns are

  • Inverted pyramid
  • Left inverted pyramid
  • Right inverted pyramid

There will be two different approaches based on alignments i.e. horizontal and vertical:

Table of Content

  • Horizontally Inverted Pyramid
  • Vertically Inverted Pyramid

Similar Reads

Horizontally Inverted Pyramid

Define a function that takes the number of rows as input.Inside the function, we use nested loops to iterate through each row and each position within the row.The first loop will iterate through each row.Second loop will print spaces before the stars based on the current row numberIf it’s the first row or the first or last position in the row, we print a star.Otherwise, we print a space.Finally, we print each row to create the inverted hollow star pyramid....

Vertically Inverted Pyramid

Vertically inverted pyramids include the Right inverted and Left inverted pyramidsTo achieve this we will use 2 Nested for loops to cover two halves of the pyramid i.e. right angle triangle and inverted right angle triangle and execute from 1 to N and back to 1.First or outer loop will iterate N times for iterating rows and initiate new string.Inner loop will also iterate and time and contain the right angle triangle .i.e.,row + column == N+1column == NAfter completion of both the loops we will get the complete vertically inverted hollow pattern....