-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharbol.js
41 lines (32 loc) · 1.26 KB
/
arbol.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
//¡Es hora de poner la navidad en casa!
/* ¡Es hora de poner el árbol de navidad en casa! 🎄
Para ello vamos a crear una función que recibe la altura del árbol,
que será un entero positivo del 1 a, como máximo, 100. */
const XMAS_TREE_SYMBOLS = Object.freeze({
EMPTY: '_',
LEAF: '*',
TRUNK: '#',
})
const XMAS_TRUNK_HEIGHT = 2
export default function createXmasTree(height) {
return (createXmasTreeTop(height) + createXmasTrunk(height)).trim()
}
function createXmasTreeTop(height) {
let treeTop = ''
for (let i = height; i > 0; --i) {
treeTop += createXmaxTreeTopLevel(height, i)
}
return treeTop
}
function createXmaxTreeTopLevel(height, level) {
const emptySpace = fillString(level - 1, XMAS_TREE_SYMBOLS.EMPTY)
const leavesPart = fillString(height - level, XMAS_TREE_SYMBOLS.LEAF)
return emptySpace + leavesPart + XMAS_TREE_SYMBOLS.LEAF + leavesPart + emptySpace + '\n'
}
function fillString(size, filler) {
return Array(size).fill(filler).join('')
}
function createXmasTrunk(height) {
const emptySpace = fillString(height - 1, XMAS_TREE_SYMBOLS.EMPTY)
return [...Array(XMAS_TRUNK_HEIGHT)].reduce(trunk => trunk += emptySpace + XMAS_TREE_SYMBOLS.TRUNK + emptySpace + '\n', '')
}