Vertically Inverted Pyramid
- Vertically inverted pyramids include the Right inverted and Left inverted pyramids
- To 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+1
- column == N
- After completion of both the loops we will get the complete vertically inverted hollow pattern.
Example 1: The following code demonstrates the Right inverted pyramid.
let n = 5;
for (let i = 1; i <= n; i++) {
let str = ''
for(let j = 1; j <= n ; ++j){
if(i==j||j==1)
str+='* ';
else
str+=' ';
}
console.log(str);
}
for (let i = n-1; i >= 1; i--) {
let str = ''
for(let j = 1; j <= n ; ++j){
if(i==j||j==1)
str+='* ';
else
str+=' ';
}
console.log(str);
}
Output
* * * * * * * * * * * * * * * *
Example 2: The following code demonstrates the Left inverted pyramid.
let n = 5;
for (let i = 1; i <= n; i++) {
let str = ''
for(let j = 1; j <= n ; ++j){
if(i+j==1+n||j==n)
str+='* ';
else
str+=' ';
}
console.log(str);
}
for (let i = n-1; i >= 1; i--) {
let str = ''
for(let j = 1; j <= n ; ++j){
if(i+j==n+1||j==n)
str+='* ';
else
str+=' ';
}
console.log(str);
}
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