JavaScript Program to Print Diamond Pattern

Introduction

A diamond star pattern is formed by printing stars (*) in the shape of a diamond. The pattern consists of two parts: an upper pyramid and a lower inverted pyramid. This is a good exercise to practice loops, conditional statements, and formatting in JavaScript.

Problem Statement

Create a JavaScript program that:

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

Example:

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

Solution Steps

  1. Input the Number of Rows: The user specifies the number of rows for the upper part of the diamond.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces for both the upper and lower parts of the diamond.
  3. Display the Diamond Pattern: Print stars in increasing order for the upper part and decreasing order for the lower part, with spaces for alignment.

JavaScript Program

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

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

// Step 3: Print the lower part of the diamond (inverted pyramid)
for (let i = rows - 1; i >= 1; i--) {
    let output = '';
    
    // Print spaces for alignment
    for (let j = 1; j <= rows - i; j++) {
        output += ' ';
    }
    
    // Print stars for the current row
    for (let k = 1; 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 for the number of rows for the upper part of the diamond. This input is converted to an integer using parseInt().

Step 2: Print the Upper Part of the Diamond

  • The outer loop handles the rows for the upper part of the diamond.
    • The first inner loop prints spaces to align the stars in the center.
    • The second inner loop prints stars (*) in increasing order, with the number of stars following the formula 2 * i - 1.

Step 3: Print the Lower Part of the Diamond

  • The second outer loop handles the rows for the lower part of the diamond.
    • The first inner loop prints spaces for alignment.
    • The second inner loop prints stars in decreasing order, with the number of stars following the formula 2 * i - 1.

Output Example

For rows = 5, the output will be:

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

For rows = 4, the output will be:

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

Conclusion

This JavaScript program prints a diamond pattern using stars (*). The program uses nested loops to print both the upper and lower parts of the diamond, with spaces for alignment. This exercise helps in practicing loop control, conditional logic, and formatting output in JavaScript.

Comments