Introduction
An hourglass star pattern consists of stars arranged in the shape of an hourglass. It has two parts: an inverted pyramid (upper half) and a regular pyramid (lower half). This pattern is useful for practicing loops, conditional logic, and formatting in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts the number of rows for the hourglass pattern.
- Prints an hourglass-shaped star pattern using stars (
*
).
Example:
- Input:
rows = 5
- Output:
********* ******* ***** *** * *** ***** ******* *********
Solution Steps
- Input the Number of Rows: The user specifies the number of rows for the hourglass pattern (half the height of the hourglass).
- 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).
- Display the Hourglass 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 hourglass pattern
let rows = parseInt(prompt("Enter the number of rows: "));
// Step 2: Print the upper part of the hourglass (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 hourglass (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 hourglass pattern. This number will determine the height of half the hourglass.
Step 2: Print the Upper Part of the Hourglass (Inverted Pyramid)
- The first loop handles the rows for the upper inverted pyramid.
- The first inner loop prints spaces to align the stars.
- The second inner loop prints stars (
*
) in decreasing order to create the inverted pyramid shape.
Step 3: Print the Lower Part of the Hourglass (Regular Pyramid)
- The second loop handles the rows for the regular pyramid (lower part).
- The first inner loop prints spaces for alignment.
- The second inner loop prints stars in increasing order to form the lower pyramid.
Output Example
For rows = 5
, the output will be:
*********
*******
*****
***
*
***
*****
*******
*********
For rows = 4
, the output will be:
*******
*****
***
*
***
*****
*******
Conclusion
This JavaScript program prints an hourglass star pattern using nested loops to create both the inverted and regular pyramids. Stars are printed with spaces for alignment, forming the hourglass shape. This exercise is helpful for practicing loop control, alignment, and output formatting in JavaScript.
Comments
Post a Comment
Leave Comment