-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.js
48 lines (43 loc) · 1.35 KB
/
solution.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
43
44
45
46
47
48
const createXmasTree = height => {
const [TREE_BODY_SYMBOL, TREE_TRUNK_SYMBOL, BACKGROUND_SYMBOL] = ['*', '#', '_'];
const treeTrunkHeight = 2;
let tree = '';
for (let i = 1; i <= height + treeTrunkHeight; i++) {
for (let j = 1; j <= height + height - 1; j++) {
// Tree part
if (i <= height) {
if (j <= height - i || j >= height + i) {
tree += BACKGROUND_SYMBOL;
} else {
tree += TREE_BODY_SYMBOL;
}
}
// Trunk part
if (i > height) {
if (j === (height * 2) / 2) {
tree += TREE_TRUNK_SYMBOL;
} else {
tree += BACKGROUND_SYMBOL;
}
}
}
tree += '\n';
}
return tree.trim();
};
const createXmasTreeAlt = height => {
const [TREE_BODY_SYMBOL, TREE_TRUNK_SYMBOL, BACKGROUND_SYMBOL] = ['*', '#', '_'];
const treeBody = Array.from({ length: height }, (_, index) =>
TREE_BODY_SYMBOL.repeat(2 * index + 1)
.padStart(index + height, BACKGROUND_SYMBOL)
.padEnd(height * 2 - 1, BACKGROUND_SYMBOL)
.concat('\n')
).join('');
const treeTrunkHeight = 2;
const treeTrunk = TREE_TRUNK_SYMBOL.padStart(height, BACKGROUND_SYMBOL)
.padEnd(height * 2 - 1, BACKGROUND_SYMBOL)
.concat('\n')
.repeat(treeTrunkHeight);
return treeBody.concat(treeTrunk).trim();
};
export { createXmasTree, createXmasTreeAlt };