0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Diamond Pattern

You can print a diamond pattern in JavaScript by using nested for loops to print spaces and stars in the right order, like this: for(let i=1; i<=n; i++){ console.log(' '.repeat(n-i) + '*'.repeat(2*i-1)); } for(let i=n-1; i>0; i--){ console.log(' '.repeat(n-i) + '*'.repeat(2*i-1)); }.
📋

Examples

Inputn = 1
Output*
Inputn = 3
Output * *** ***** *** *
Inputn = 5
Output * *** ***** ******* ********* ******* ***** *** *
🧠

How to Think About It

To print a diamond pattern, first print the top half by increasing the number of stars each line and decreasing spaces before them. Then print the bottom half by decreasing stars and increasing spaces. Use loops to control spaces and stars count for each line.
📐

Algorithm

1
Get input number n for the diamond's half height.
2
For each line from 1 to n, print spaces (n - line) and then stars (2 * line - 1).
3
For each line from n-1 down to 1, print spaces (n - line) and then stars (2 * line - 1).
4
Print each line on a new line to form the diamond shape.
💻

Code

javascript
const n = 5;
for(let i = 1; i <= n; i++) {
  console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
for(let i = n - 1; i > 0; i--) {
  console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
Output
* *** ***** ******* ********* ******* ***** *** *
🔍

Dry Run

Let's trace n=3 through the code to see how the diamond is printed.

1

Top half line 1

i=1, spaces = 3-1=2, stars = 2*1-1=1, line = ' *'

2

Top half line 2

i=2, spaces = 3-2=1, stars = 3, line = ' ***'

3

Top half line 3

i=3, spaces = 3-3=0, stars = 5, line = '*****'

4

Bottom half line 1

i=2, spaces = 3-2=1, stars = 3, line = ' ***'

5

Bottom half line 2

i=1, spaces = 3-1=2, stars = 1, line = ' *'

LineSpacesStarsOutput
121 *
213 ***
305*****
413 ***
521 *
💡

Why This Works

Step 1: Print top half

The first loop prints lines with increasing stars and decreasing spaces using repeat to create the pattern.

Step 2: Print bottom half

The second loop prints lines with decreasing stars and increasing spaces to complete the diamond shape.

Step 3: Use of string repeat

Using ' '.repeat() and '*'.repeat() makes it easy to print the exact number of spaces and stars needed.

🔄

Alternative Approaches

Using while loops
javascript
let i = 1;
while(i <= n) {
  console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
  i++;
}
i = n - 1;
while(i > 0) {
  console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
  i--;
}
While loops do the same job but are less concise than for loops.
Using recursion
javascript
function printDiamond(n, i = 1) {
  if(i > n) return;
  console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
  printDiamond(n, i + 1);
  if(i < n) console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
printDiamond(5);
Recursion prints the diamond by calling itself for top and bottom halves, but can be harder to understand.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses two loops each running up to n, and inside each loop it prints up to 2*n characters, so overall time is O(n^2).

Space Complexity

Only a few variables are used and strings are printed directly, so extra space is O(1).

Which Approach is Fastest?

For loops are fastest and simplest; recursion adds overhead and while loops are similar but less concise.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simple and clear code
While loopsO(n^2)O(1)Alternative loop style
RecursionO(n^2)O(n)Learning recursion, less efficient
💡
Use ' '.repeat() and '*'.repeat() to easily control spaces and stars in each line.
⚠️
Beginners often forget to print the bottom half of the diamond or get the number of spaces wrong, breaking the shape.