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.

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

Javascript
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

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