JavaScript Program to Print Sandglass Star Pattern

Introduction

A sandglass star pattern consists of stars (*) arranged in an hourglass or sandglass shape. The pattern starts with a full row of stars, decreases in each row until it reaches one star, and then increases back to a full row. This exercise is useful for practicing loops and formatting in JavaScript.

Problem Statement

Create a JavaScript program that:

  • Accepts the number of rows for the sandglass pattern.
  • Prints a sandglass pattern using stars (*).

Example:

  • Input: rows = 5
  • Output:
    *********
     *******
      *****
       ***
        *
       ***
      *****
     *******
    *********
    

Solution Steps

  1. Input the Number of Rows: The user specifies how many rows the sandglass pattern should have (the height of half the sandglass).
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces for both the inverted pyramid (upper part) and the regular pyramid (lower part).
  3. Display the Sandglass Pattern: Print stars in decreasing order for the upper part and increasing order for the lower part, with spaces for alignment.

JavaScript Program

// Step 1: Input the number of rows for the sandglass pattern
let rows = parseInt(prompt("Enter the number of rows: "));

// Step 2: Print the upper part of the sandglass (inverted pyramid)
for (let i = rows; i >= 1; i--) {
    let output = '';
    
    // Print leading spaces for alignment
    for (let j = 0; j < rows - i; j++) {
        output += ' ';
    }
    
    // Print stars for the current row
    for (let k = 0; k < 2 * i - 1; k++) {
        output += '*';
    }
    
    // Print the output for the current row
    console.log(output);
}

// Step 3: Print the lower part of the sandglass (regular pyramid)
for (let i = 2; i <= rows; i++) {
    let output = '';
    
    // Print leading spaces for alignment
    for (let j = 0; j < rows - i; j++) {
        output += ' ';
    }
    
    // Print stars for the current row
    for (let k = 0; k < 2 * i - 1; k++) {
        output += '*';
    }
    
    // Print the output for the current row
    console.log(output);
}

Explanation

Step 1: Input the Number of Rows

  • The program starts by asking the user to input the number of rows for the sandglass pattern. This number determines the height of the upper inverted pyramid and lower pyramid.

Step 2: Print the Upper Part of the Sandglass (Inverted Pyramid)

  • The first outer loop handles the rows for the inverted pyramid, which forms the upper part of the sandglass.
    • The first inner loop prints spaces to align the stars.
    • The second inner loop prints stars (*) in decreasing order based on the current row number.

Step 3: Print the Lower Part of the Sandglass (Regular Pyramid)

  • The second outer loop handles the rows for the regular pyramid, which forms the lower part of the sandglass.
    • The first inner loop prints spaces for alignment.
    • The second inner loop prints stars in increasing order, forming the lower pyramid.

Output Example

For rows = 5, the output will be:

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

For rows = 4, the output will be:

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

Conclusion

This JavaScript program prints a sandglass star pattern using nested loops to create both an inverted and a regular pyramid. Stars are printed with spaces for alignment, forming the sandglass shape. This exercise helps in practicing loop control, alignment, and output formatting in JavaScript.

Comments