To print a hollow pyramid pattern in JavaScript, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
function printHollowPyramid(rows) {
for (let i = 1; i <= rows; i++) {
let pattern = '';
for (let j = i; j < rows; j++) {
pattern += ' ';
}
for (let k = 1; k < (2 * i); k++) {
if (k === 1 || k === (2 * i - 1) || i === rows) {
pattern += '* ';
} else {
pattern += ' ';
}
}
console.log(pattern);
}
}
printHollowPyramid(5);
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in JavaScript language with well detailed examples.