Created
December 29, 2024 10:03
-
-
Save thehypergraph/d4819440be692b66c2075d9327116535 to your computer and use it in GitHub Desktop.
Christmas Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function christmasTree(w: number, ch: string, sep: string, isEmoji: boolean = false) { | |
if (w % 2 !== 0) { | |
console.log('Please pick an even number for the height of the tree.') | |
return | |
} | |
const spacingMultiplier = isEmoji ? 2 : 1 // Emoji characters are roughly 2 spaces wide | |
for (let i = 1; i < w; i += 2) { | |
const spacing = (w - i - 1) / 2 | |
const side = sep.repeat(spacing * spacingMultiplier) | |
const row = side + ch.repeat(i) + side | |
console.log(row) | |
} | |
} | |
// For emoji with space separator, set isEmoji to true | |
// christmasTree(20, '🎄', ' ', true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment