JavaScript Program for Hollow Pyramid Star Pattern

In this article, we will demonstrate how to create Hollow Pyramid Star Patterns in JavaScript.
Possible Hollow Pyramid Star Patterns are
- Upper pyramid
- Inverted pyramid
- Left inverted pyramid
- Right inverted pyramid
Approach
There will be two different approaches based on alignments i.e. horizontal and vertical:
- Horizontally Inverted Pyramid
- Vertically Inverted Pyramid
Horizontally Inverted Pyramid
- This type includes the upper pyramid and the inverted pyramid.
- To achieve this we will use Nested For loops.
- The first loop will iterate N times for row iteration and initiate a new row string.
- Second loop will perform Double iteration and will contain the condition to print the required star pattern i.e.,
- row + column == N +1
- row+ N == coulmn + 1
- row = = N
Example 1: Upper pyramid
Javascript
let n = 5; for (let i = 1; i <= n; i++) {     let str = ''    for(let j = 1; j <= 2*n-1 ; ++j){         if(i+j == n +1||(i+n==j+1) ||i==n)         str+='* ';         else        str+=' ';     }     console.log(str); } |
Output
*
* *
* *
* *
* * * * * * * * *
Example 2: Inverted pyramid
Javascript
let n = 5; for (let i = n; i >= 1; i--) {       let str = ''    for(let j = 1; j <= 2*n-1 ; ++j){         if(i+j == n +1||(i+n==j+1) ||i==n)         str+='* ';         else        str+=' ';     }     console.log(str); } |
Output
* * * * * * * * *
* *
* *
* *
*
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.
Example1: 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: 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
*
* *
* *
* *
* *
* *
* *
* *
*
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



