Star pyramid
Given a row count n, return a centred pyramid of stars as an array of n strings. Row i holds 2i − 1 stars, so the counts run 1, 3, 5 and stay odd, and it carries n − i spaces in front of them. There is no padding after the stars, so every row ends on a *.
Examples
Example 1
- Input
- n = 5
- Output
* *** ***** ******* *********
Five rows: 1, 3, 5, 7 and 9 stars behind 4, 3, 2, 1 and 0 spaces.[" *"," ***"," *****"," *******","*********"]
Example 2
- Input
- n = 3
- Output
* *** *****
The last row of a three-row pyramid has no padding left and 2 × 3 − 1 = 5 stars.[" *"," ***","*****"]
The Code
function pyramid(n) {
const rows = [];
for (let row = 1; row <= n; row++) {
let line = "";
for (let space = 1; space <= n - row; space++) {
line += " ";
}
for (let star = 1; star <= row * 2 - 1; star++) {
line += "*";
}
rows.push(line);
}
return rows;
}
pyramid(5);The first 16 calls, of 51. This one does not fit on a page.
Explanation
Two counts move per row, against each other: a row gains two stars as it gives up one space. The star added on the left takes the place the padding gave up, which is what holds the tip in one column. Each space is drawn as · in the rows below.
- 1
rowis 1. The first inner loop runs whilespaceis at mostn - row, so four spaces go ontoline; the second runs whilestaris at mostrow * 2 - 1, so one star follows them. Only then islinepushed.····*0n - row=4row * 2 - 1=1
rows.push("····*")
- 2
rowis 2.n - rowhas dropped to 3 whilerow * 2 - 1has risen to 3, so the row gives up a space and takes two stars — one on each side of the star already there.····*0···***1n - row=3row * 2 - 1=3
rows.push("···***")
- 3
rowis 3: two spaces and five stars. The two counts always move together, one down by one and the other up by two, which is why the left edge slopes by a single column a row.····*0···***1··*****2n - row=2row * 2 - 1=5
rows.push("··*****")
- 4
rowis 4: one space and seven stars.····*0···***1··*****2·*******3n - row=1row * 2 - 1=7
rows.push("·*******")
- 5
rowis 5, son - rowis 0 and the first inner loop does not run at all. The second appends5 * 2 - 1= 9 stars, and the outer loop ends withrowsfive strings long.····*0···***1··*****2·*******3*********4n - row=0row * 2 - 1=9
return rows → 5 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 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.
- Butterfly Two wings of stars with a gap between them that closes and reopens.
- Floyd’s triangle One counter that never resets, spilled across widening rows.