To print a Hollow Diamond Pattern in TypeScript, we use nested loops to manage spaces and stars.
In this example,
// Print Hollow Diamond Pattern in TypeScript
function printHollowDiamond(numRows: number): string {
const diamond: string[] = [];
for (let row = 1; row <= numRows; row++) {
let pattern = '';
for (let space = 1; space <= numRows - row; space++) {
pattern += ' ';
}
for (let star = 1; star <= 2 * row - 1; star++) {
if (star === 1 || star === 2 * row - 1) {
pattern += '*';
} else {
pattern += ' ';
}
}
diamond.push(pattern);
}
for (let row = numRows - 1; row >= 1; row--) {
let pattern = '';
for (let space = 1; space <= numRows - row; space++) {
pattern += ' ';
}
for (let star = 1; star <= 2 * row - 1; star++) {
if (star === 1 || star === 2 * row - 1) {
pattern += '*';
} else {
pattern += ' ';
}
}
diamond.push(pattern);
}
return diamond.join('\n');
}
console.log(printHollowDiamond(5));
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Diamond Pattern in TypeScript language with well detailed examples.