Horje
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

        * 
      * * 
    *   * 
  *     * 
*       * 
  *     * 
    *   * 
      * * 
        * 



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Sort Words in Alphabetical Order JavaScript Program to Sort Words in Alphabetical Order
JavaScript Program to Shuffle Deck of Cards JavaScript Program to Shuffle Deck of Cards
JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case
Check if a String Contains any Whitespace Characters using JavaScript Check if a String Contains any Whitespace Characters using JavaScript
JavaScript Program to Sort an Array which Contain 1 to n Values JavaScript Program to Sort an Array which Contain 1 to n Values

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10