How to print Hollow Pyramid Pattern in JavaScript - Step by Step Examples



How to print Hollow Pyramid Pattern in JavaScript ?

Answer

To print a hollow pyramid pattern in JavaScript, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.



✐ Examples

1 Hollow Pyramid Pattern (Height: 5) in JavaScript

In this example,

  1. Set the number of rows for the pyramid.
  2. Use nested loops: one for rows, one for spaces, and one for stars.
  3. In the inner loops, print spaces for the spaces between stars and print stars for the pyramid edges.
  4. Adjust conditions to print stars only at the pyramid edges and spaces elsewhere to create the hollow effect.
  5. Print each row to create the hollow pyramid pattern.

JavaScript Program

function printHollowPyramid(rows) {
    for (let i = 1; i <= rows; i++) {
        let pattern = '';
        for (let j = i; j < rows; j++) {
            pattern += '  ';
        }
        for (let k = 1; k < (2 * i); k++) {
            if (k === 1 || k === (2 * i - 1) || i === rows) {
                pattern += '* ';
            } else {
                pattern += '  ';
            }
        }
        console.log(pattern);
    }
}
printHollowPyramid(5);

Output

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

Summary

In this tutorial, we learned How to print Hollow Pyramid Pattern in JavaScript language with well detailed examples.




More JavaScript Pattern Printing Tutorials

  1. How to print Left Half Pyramid Pattern in JavaScript ?
  2. How to print Right Half Pyramid Pattern in JavaScript ?
  3. How to print Pyramid Pattern in JavaScript ?
  4. How to print Rhombus Pattern in JavaScript ?
  5. How to print Diamond Pattern in JavaScript ?
  6. How to print Hour Glass Pattern in JavaScript ?
  7. How to print Hollow Square Pattern in JavaScript ?
  8. How to print Hollow Pyramid Pattern in JavaScript ?
  9. How to print Hollow Inverted Pyramid Pattern in JavaScript ?
  10. How to print Hollow Diamond Pattern in JavaScript ?
  11. How to print Floyd's Trianlge Pattern in JavaScript ?
  12. How to print Pascal's Triangle Pattern in JavaScript ?