Skip to content

Exercise 10 - Christmas Tree

function printTree(maxWidth) {
printTop(maxWidth);
printTrunk(maxWidth);
}
function printTop(maxWidth) {
const height = maxWidth / 2;
let width = 1;
for (let i = 0; i < height; i++) {
let line = "";
let side = (maxWidth - width) / 2;
for (let j = 0; j < side; j++) {
line += " ";
}
for (let j = 0; j < width; j++) {
if (Math.random() > 0.9) {
line += "@";
} else {
line += "*";
}
}
console.log(line);
width += 2;
}
}
function printTrunk(maxWidth) {
let line = "";
for (let i = 0; i < (maxWidth - 1) / 2; i++) {
line += " ";
}
line += "*";
console.log(line);
console.log(line);
}
const maxWidth = 21;
printTree(maxWidth);