To print a pyramid pattern in JavaScript, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
n
to represent the number of rows in the pyramid.console.log()
to move to the next line.function printPyramid(n) {
for (let i = 1; i <= n; i++) {
let str = ' '.repeat(n - i);
str += '*'.repeat(2 * i - 1);
console.log(str);
}
}
printPyramid(5);
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in JavaScript language with well detailed examples.