No. 29 / Advanced
Hollow Hourglass
Outputn = 5
*********
* *
* *
* *
*
* *
* *
* *
*********9 rows
Controls
5
Character
Normal
How it works
A hollow inverted pyramid on top of a hollow pyramid. Only the outline is kept, and the first and last rows are filled completely.
i 1 → nspaces i − 1stars 2(n − i) + 1
i 2 → nspaces n − istars 2i − 1
pattern.js
1
const n = 5;2
3
for (let i = 1; i <= n; i++) {4
let row = "";5
for (let s = 1; s <= i - 1; s++) {6
row += " ";7
}8
for (let j = 1; j <= 2 * (n - i) + 1; j++) {9
if (j === 1 || j === 2 * (n - i) + 1 || i === 1) {10
row += "*";11
} else {12
row += " ";13
}14
}15
console.log(row);16
}17
18
for (let i = 2; i <= n; i++) {19
let row = "";20
for (let s = 1; s <= n - i; s++) {21
row += " ";22
}23
for (let j = 1; j <= 2 * i - 1; j++) {24
if (j === 1 || j === 2 * i - 1 || i === n) {25
row += "*";26
} else {27
row += " ";28
}29
}30
console.log(row);31
}