Pyramid Star Pattern Program

Pyramid Star Pattern Program

February 10, 2023


Learn How to make Pyramid Star Pattern Program in JavaScript

today I'm going to teach you how to make Pyramid Star Pattern Program using JavaScript. There are two ways to make this program.

First way:

  • We need to make four loops for this.
  • The first loop is the main loop deciding how many rows we want.
  • The remaining three loops will be inside the first loop.
  • the second loop will be to print the empty space
  • The third and fourth loop print * .

Add JavaScript Code In Your File:

Example:

var x = 5;

for (let i = 1; i <= x; i++) {

	for (let j = 1; j <= x - i; j++) {
		document.write("&nbsp;&nbsp;");
	}
	for (let k = 1; k <= i; k++) {
		document.write("*");
	}

	for (let l = 1; l <= i - 1; l++) {
		document.write("*");
	}

	document.write("<br/>");

}
Try this code

Second Way:

  • For this we just have to make three loops.
  • The first loop is the main loop deciding how many rows we want.
  • The remaining two loops will be inside the first loop.
  • the second loop will be to print the empty space
  • The third loop print * .

 Add JavaScript code in your file.

Example:

var x = 5, m=1;
for (let i = 1; i <= x; i++) {
	for (let j = 1; j <= x - i; j++) {
		document.write("&nbsp;&nbsp;");
	}
  
	for (let k = 1; k <= m; k++) {
		document.write("*");
	}
  
	document.write("<br/>");
  	m=m+2;
}
Try this code