No. 33 / Advanced
Christmas Tree
Outputn = 5
*
***
*****
*******
*********
***
*****
*******
*********
***********
*****
*******
*********
***********
*************
***
***17 rows
Controls
5
Character
Normal
How it works
Three pyramids stacked on top of each other, each one starting two stars wider than the previous one. The spaces before each row keep everything centered, and a two row trunk finishes the tree.
i 1 → nspaces n + 2 − istars 2i − 1
i 1 → nspaces n + 1 − istars 2i + 1
i 1 → nspaces n − istars 2i + 3
i 1 → 2spaces nstars 3
pattern.js
1
const n = 5;2
3
for (let i = 1; i <= n; i++) {4
let row = "";5
for (let s = 1; s <= n + 2 - i; s++) {6
row += " ";7
}8
for (let j = 1; j <= 2 * i - 1; j++) {9
row += "*";10
}11
console.log(row);12
}13
14
for (let i = 1; i <= n; i++) {15
let row = "";16
for (let s = 1; s <= n + 1 - i; s++) {17
row += " ";18
}19
for (let j = 1; j <= 2 * i + 1; j++) {20
row += "*";21
}22
console.log(row);23
}24
25
for (let i = 1; i <= n; i++) {26
let row = "";27
for (let s = 1; s <= n - i; s++) {28
row += " ";29
}30
for (let j = 1; j <= 2 * i + 3; j++) {31
row += "*";32
}33
console.log(row);34
}35
36
for (let i = 1; i <= 2; i++) {37
let row = "";38
for (let s = 1; s <= n; s++) {39
row += " ";40
}41
for (let j = 1; j <= 3; j++) {42
row += "*";43
}44
console.log(row);45
}