LoopsX Studio
No. 31 / Advanced

Hollow Hexagon

Outputn = 5
    *****
   *     *
  *       *
 *         *
*           *
 *         *
  *       *
   *     *
    *****
9 rows
Controls
5
Character
Normal
How it works

Same shape as the hexagon, but only the outline is kept: each row prints its first and last star, and the top and bottom rows are filled completely.

i 1 → nspaces n − istars n + 2i − 2
i 1 → n − 1spaces istars 3n − 2 − 2i
pattern.js
1const n = 5;
2
3for (let i = 1; i <= n; i++) {
4 let row = "";
5 for (let s = 1; s <= n - i; s++) {
6 row += " ";
7 }
8 for (let j = 1; j <= n + 2 * i - 2; j++) {
9 if (j === 1 || j === n + 2 * i - 2 || i === 1) {
10 row += "*";
11 } else {
12 row += " ";
13 }
14 }
15 console.log(row);
16}
17
18for (let i = 1; i <= n - 1; i++) {
19 let row = "";
20 for (let s = 1; s <= i; s++) {
21 row += " ";
22 }
23 for (let j = 1; j <= 3 * n - 2 - 2 * i; j++) {
24 if (j === 1 || j === 3 * n - 2 - 2 * i || i === n - 1) {
25 row += "*";
26 } else {
27 row += " ";
28 }
29 }
30 console.log(row);
31}