Butterfly pattern
Given a half-height n, return the butterfly as an array of 2n − 1 strings: two triangles of stars facing outwards with a gap between them. Row i of the upper half holds i stars, then 2(n − i) spaces, then i stars again, and the rows after the middle repeat the upper half in reverse. Every row is the same width, 2n characters, and the middle row has no gap at all.
Examples
Example 1
- Input
- n = 4
- Output
* * ** ** *** *** ******** *** *** ** ** * *
Eight characters on every row: 1 + 6 + 1, then 2 + 4 + 2, then 3 + 2 + 3, then 8.["* *","** **","*** ***","********","*** ***","** **","* *"]
Example 2
- Input
- n = 2
- Output
* * **** * *
Three rows of four characters, and the middle row spends all four on stars.["* *","****","* *"]
The Code
function butterfly(n) {
const rows = [];
function wing(stars) {
let line = "";
for (let i = 0; i < stars; i++) line += "*";
for (let i = 0; i < (n - stars) * 2; i++) line += " ";
for (let i = 0; i < stars; i++) line += "*";
return line;
}
for (let row = 1; row <= n; row++) {
rows.push(wing(row));
}
for (let row = n - 1; row >= 1; row--) {
rows.push(wing(row));
}
return rows;
}
butterfly(4);The first 18 calls, of 93. This one does not fit on a page.
Explanation
Each row spends the same 2n characters on two wings and the gap between them. Whatever the wings take, the gap gives up, so the outer edges stay straight while the inner ones close. Each space is drawn as · in the rows below.
- 1
rowis 1, sowing(1)runs: one*, then(n - stars) * 2= 6 spaces, then one*again. The helper returns the finished string and the loop pushes it.*······*0stars=1(n - stars) * 2=6
rows.push("*······*")
- 2
rowis 2 —wing(2). Each wing takes one more star, so the gap has to give up two, and the row is still eight characters wide.*······*0**····**1stars=2(n - stars) * 2=4
rows.push("**····**")
- 3
rowis 3 —wing(3): three stars, two spaces, three stars.*······*0**····**1***··***2stars=3(n - stars) * 2=2
rows.push("***··***")
- 4
rowis 4, which isn, so(n - stars) * 2is 0 and the middle loop ofwingdoes not run: the two wings meet.row++then ends the first loop.*······*0**····**1***··***2********3stars=4(n - stars) * 2=0
rows.push("********")
- 5
The second loop opens at
n - 1, which is 3. Opening atnwould callwing(4)again and the butterfly would carry two closed rows.*······*0**····**1***··***2********3***··***4row=3rows.length=4
rows.push("***··***")
- 6
rowcounts down through 2 and 1, callingwingwith the same numbers the first loop used, so the rows come back out in reverse. At 0 it failsrow >= 1and the seven strings are returned.*······*0**····**1***··***2********3***··***4**····**5*······*6row=2 then 1rows.length=5
return rows → 7 rows
More like this
All star & number patterns examples (12) →- Star triangle The inner loop runs as far as the row number — that is the whole idea.
- Inverted triangle Same triangle, counted downwards instead of up.
- Star pyramid Leading spaces do the centring; stars go up in odd numbers.
- Star diamond A pyramid, then the same pyramid upside down — minus the shared row.
- Hollow square Fill only the border — every interior cell is a space.
- Floyd’s triangle One counter that never resets, spilled across widening rows.