-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-10.js
42 lines (34 loc) · 997 Bytes
/
day-10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function createChristmasTree(ornaments, height) {
let tree = "";
let ornamentsPrinted = 0;
// Creating a repeated ornaments to Add to the line
for (let x = 0; x <= height; x++) {
ornamentsPrinted = ornamentsPrinted + x;
}
// console.log(ornamentsPrinted)
let repeatString = ornaments.repeat(ornamentsPrinted);
for (let i = 1; i <= height; i++) {
let line = "";
// Add spaces to center the ornaments
for (let j = 0; j < height - i; j++) {
line += " ";
}
// Add ornaments to the line
for (let k = 0; k < i; k++) {
let o = 0
// line += " " + repeatString[o+1];
o++
}
line += " " + repeatString[i];
// Remove trailing space and add the line to the tree
tree += line + "\n";
}
// Add trunk to the tree
tree += " ".repeat(height) + "|\n";
return tree;
}
// Example usage:
const tree1 = createChristmasTree("123", 4);
console.log(tree1);
const tree2 = createChristmasTree("*@os", 3);
console.log(tree2);