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.
// 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