-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (21 loc) · 712 Bytes
/
index.js
File metadata and controls
27 lines (21 loc) · 712 Bytes
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
/**
* @param {number} height
* @param {string} ornament
* @returns {string}
*/
export function createXmasTree (height, ornament) {
const TRUNK_SYMBOL = '#'
const tree = []
for (let i = 0; i < height; i++) {
const underscores = '_'.repeat(height - i - 1)
const ornaments = ornament.repeat(i)
const leftHalf = [underscores, ornaments].join('')
const rightHalf = [ornaments, underscores].join('')
tree.push([leftHalf, ornament, rightHalf].join(''))
}
const trunkUnderscores = '_'.repeat(height - 1)
const halfTrunk = [trunkUnderscores, TRUNK_SYMBOL, trunkUnderscores].join('')
const trunk = [halfTrunk, halfTrunk].join('\n')
tree.push(trunk)
return tree.join('\n')
}