Introduction
A butterfly star pattern consists of two symmetrical halves: the upper half where the number of stars increases with each row, and the lower half where the number of stars decreases with each row. This pattern looks like a butterfly with stars on both wings. It is a great exercise for practicing nested loops and output formatting in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts the number of rows for the butterfly pattern.
- Prints a butterfly-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 butterfly pattern.
- Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces.
- Display the Butterfly Pattern: Print stars in increasing order for the upper part and decreasing order for the lower part, with spaces in the middle to separate the two halves of the butterfly.
JavaScript Program
// Step 1: Input the number of rows for the butterfly pattern
let rows = parseInt(prompt("Enter the number of rows: "));
// Step 2: Print the upper part of the butterfly
for (let i = 1; i <= rows; i++) {
let output = '';
// Print stars on the left side
for (let j = 1; j <= i; j++) {
output += '*';
}
// Print spaces in the middle
for (let j = 1; j <= 2 * (rows - i); j++) {
output += ' ';
}
// Print stars on the right side
for (let j = 1; j <= i; j++) {
output += '*';
}
// Print the output for the current row
console.log(output);
}
// Step 3: Print the lower part of the butterfly
for (let i = rows; i >= 1; i--) {
let output = '';
// Print stars on the left side
for (let j = 1; j <= i; j++) {
output += '*';
}
// Print spaces in the middle
for (let j = 1; j <= 2 * (rows - i); j++) {
output += ' ';
}
// Print stars on the right side
for (let j = 1; j <= i; j++) {
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 butterfly pattern. This input is converted to an integer using
parseInt()
.
Step 2: Print the Upper Part of the Butterfly
- The outer loop handles the rows for the upper part of the butterfly.
- The first inner loop prints stars (
*
) on the left side. - The second inner loop prints spaces in the middle.
- The third inner loop prints stars on the right side.
- The first inner loop prints stars (
Step 3: Print the Lower Part of the Butterfly
- The second outer loop handles the rows for the lower part of the butterfly, which is a mirror image of the upper part.
- The first inner loop prints stars on the left side.
- The second inner loop prints spaces in the middle.
- The third inner loop prints stars on the right side.
Output Example
For rows = 5
, the output will be:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
For rows = 4
, the output will be:
* *
** **
*** ***
********
*** ***
** **
* *
Conclusion
This JavaScript program prints a butterfly star pattern using nested loops to create symmetrical wings. The stars are printed on both sides of the butterfly, with spaces in between to separate the wings. This exercise is useful for practicing loop control, alignment, and output formatting in JavaScript.
Comments
Post a Comment
Leave Comment