No. 24 / Advanced
Butterfly
Outputn = 5
* * ** ** *** *** **** **** ********** **** **** *** *** ** ** * *
9 rows
Controls
5
Character
Normal
How it works
The grid has 2n − 1 rows and 2n columns. In the top half, row i fills its first i and last i cells, so the wings grow until row n. The bottom half mirrors it and the wings shrink again.
rows 2n − 1columns 2nstar when (i <= n && (j <= i || j > 2n − i)) || (i > n && (j <= 2n − i || j > i))
pattern.js
1
const n = 5;2
3
for (let i = 1; i <= 2 * n - 1; i++) {4
let row = "";5
for (let j = 1; j <= 2 * n; j++) {6
if ((i <= n && (j <= i || j > 2 * n - i)) || (i > n && (j <= 2 * n - i || j > i))) {7
row += "*";8
} else {9
row += " ";10
}11
}12
console.log(row);13
}