JavaScript Program to Print Heart Shape Star Pattern

Introduction

Printing a heart shape using stars (*) is an interesting pattern that involves carefully positioning stars and spaces. The heart shape consists of an upper part (two semicircles) and a lower triangular part, and it requires nested loops and conditional logic to print the stars correctly.

Problem Statement

Create a JavaScript program that:

  • Prints a heart-shaped star pattern using stars (*).
  • Uses nested loops and conditional logic to position the stars and spaces.

Example:

  • Output:
     ***   ***
    ***** *****
    ***********
     *********
      *******
       *****
        ***
         *
    

Solution Steps

  1. Define the Heart Pattern Size: The size controls the height of the heart shape and the number of rows for the upper and lower parts.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing stars and spaces for both the upper and lower parts of the heart.
  3. Display the Heart Shape: Use conditional logic to print stars in the correct positions.

JavaScript Program

// Step 1: Define the size of the heart pattern
let size = 6; // This size defines the height of the heart

// Step 2: Print the upper part of the heart (two semi-circles)
for (let i = size / 2; i < size; i += 2) {
    let output = '';
    
    // Print spaces before the first part of stars
    for (let j = 1; j < size - i; j += 2) {
        output += ' ';
    }
    
    // Print the first part of stars
    for (let j = 1; j <= i; j++) {
        output += '*';
    }
    
    // Print spaces between the two parts of stars
    for (let j = 1; j <= size - i; j++) {
        output += ' ';
    }
    
    // Print the second part of stars
    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 heart (inverted triangle)
for (let i = size; i >= 1; i--) {
    let output = '';
    
    // Print spaces before the stars
    for (let j = i; j < size; j++) {
        output += ' ';
    }
    
    // Print stars for the current row
    for (let j = 1; j <= (i * 2) - 1; j++) {
        output += '*';
    }
    
    // Print the output for the current row
    console.log(output);
}

Explanation

Step 1: Define the Size of the Heart Pattern

  • The variable size defines the overall size of the heart. In this example, it's set to 6, which determines the height of the heart.

Step 2: Print the Upper Part of the Heart (Two Semi-Circles)

  • The first outer loop handles printing the two semicircles at the top of the heart.
    • The first inner loop prints spaces to align the stars correctly.
    • The second inner loop prints the first set of stars.
    • Another inner loop prints spaces between the two semicircles.
    • The final inner loop prints the second set of stars.

Step 3: Print the Lower Part of the Heart (Inverted Triangle)

  • The second outer loop handles printing the inverted triangle at the bottom of the heart.
    • The first inner loop prints spaces to align the stars.
    • The second inner loop prints stars in decreasing order to form the triangular shape.

Output Example

For size = 6, the output will be:

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

For size = 8, the heart shape will be larger:

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

Conclusion

This JavaScript program prints a heart-shaped star pattern using nested loops and conditional logic. The stars are printed to form two semicircles at the top and a triangular part at the bottom. This exercise helps in practicing loops, conditional logic, and formatting output in JavaScript.

Comments