求一个可以输出圣诞树的方法JS方法?

创造一个christmasTree(height)方法,height代表圣诞书的高,返回值就是按圣诞树的形状和高出
如christmasTree(5)就会输出如下内容

https://img4.mukewang.com/5bdfec2a0001c43100760100.jpg

猛跑小猪
浏览 1364回答 1
1回答

喵喵时光机

function christmasTree(height) {&nbsp; &nbsp; const col = 1 + (height - 1) * 2,&nbsp; &nbsp; &nbsp; &nbsp; char = '*',&nbsp; &nbsp; &nbsp; &nbsp; space = ' ';&nbsp; &nbsp; var space_len = height - 1,&nbsp; &nbsp; &nbsp; &nbsp; char_len = 1,&nbsp; &nbsp; &nbsp; &nbsp; tree = '';&nbsp; &nbsp; for (let i = 0; i < height; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let spaces = space.repeat(space_len);&nbsp; &nbsp; &nbsp; &nbsp; tree += `${spaces}${char.repeat(char_len)}${spaces}\n`;&nbsp; &nbsp; &nbsp; &nbsp; char_len += 2;&nbsp; &nbsp; &nbsp; &nbsp; space_len--;&nbsp; &nbsp; }&nbsp; &nbsp; console.log(tree);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript