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(" "); } for (let k = 1; k <= i; k++) { document.write("*"); } for (let l = 1; l <= i - 1; l++) { document.write("*"); } document.write("<br/>"); }
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(" "); } for (let k = 1; k <= m; k++) { document.write("*"); } document.write("<br/>"); m=m+2; }
what is m in program? just see the pattern. The stars are being printed in increments of 2 on each line. like 1,3,5,7,.... for this situation we used m