To generate a Christmas tree in JavaScript, we can use nested loops and string concatenation to create the pattern of the tree. Here's an example code:
index.tsx729 chars37 lines
In this code, we set the height of the tree to 5. We then loop through each row of the tree, adding spaces to center the row, and asterisks for the branches. We log each row to the console using console.log()
. Finally, we add the tree trunk to the bottom of the tree by looping through two more rows and adding spaces and a single asterisk for the trunk. When this code is executed, it will print out a Christmas tree in the console.
gistlibby LogSnag