Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

How to create a diamond pattern in JavaScript

Ayush Trivedi

Overview

In this shot, we will learn how to create a diamond star pattern using JavaScript. This pattern is complex among other patterns. The logic to create a diamond pattern is combining the logic of the upside pyramid and downside pyramid.

A diamond star pattern looks like the image below:

Diamond star pattern

JavaScript code

let n = 5;
// Upside pyramid
for (let i = 1; i <= n; i++) {
  // printing spaces
  for (let j = 1; j <= n - i; j++) {
    process.stdout.write(' ')
  }
  // printing star
  for (let k = 0; k < 2 * i - 1; k++) {
    process.stdout.write('*')
  }
  console.log();
}

// downside pyramid
for (let i = 1; i <= n - 1; i++) {
  // printing spaces
  for (let j = 0; j < i; j++) {
     process.stdout.write(' ');
  }
  // printing star
  for (let k = (n - i) * 2 - 1; k > 0; k--) {
    process.stdout.write('*');
  }
  console.log();
}

Code explanation

  • In line 1, we initialize a number that is the height(rows) of the upside triangle (i.e., 5) and one less than the height of the downside triangle (i.e., 4), so the total rows(height) for the diamond is 9.

Upside pyramid

  • In line 3, we have our outer for loop, which will iterate over the height of upside triangle (n=5).

  • In line 5, we have our first nested for loop to print spaces for n-i times, where i is the outer loop iteration number. If i=3, i.e., for the third row, the number of spaces before star is 2, and so on.

  • In line 9, we have the second inner for loop to print * for (2 * i - 1) times, where i is the current iteration number of the outer loop. If i=3, i.e., for the third row, after one space it will print 5 (2 * 3 - 1) star.

  • In line 10, the standard method of JavaScript is used to print the space(*). process.stdout.write() will print the space.

  • In line 12, we used console.log() with null, as it will change to a new line. We can use process.stdout.write(’\n’) to change the line.

Downside pyramid

  • In line 16, we have have our second outer for loop, which will iterate over one less than the height of the downside pyramid (that is, n-1 = 4 times, here n=5).

  • In line 18, our first inner for loop prints spaces for i times, where i is the outer loop iteration number. If i=3, i.e., for the third row, the number of space before the star is 3, for i=4 the space is 4, and so on.

  • In line 19, the standard method of JavaScript is used to print the space (" "). process.stdout.write() will print the space.

  • In line 22, the second nested for loop prints * for 2 * (n - i) - 1 times, where i is the current iteration number of outer loop. If i=3, i.e., for the third row, after 3 spaces it will print 3 star (2 * (5 - 3) - 1).

  • In line 23, the standard method of JavaScript is used to print the space(*). process.stdout.write() will print the space.

  • In line 25, we used console.log() with null, as it will change to a new line. We can use process.stdout.write(’\n’) to change the line.

Output

We have completed the task to create a diamond star pattern in JavaScript.

RELATED TAGS

javascript
communitycreator
RELATED COURSES

View all Courses

Keep Exploring