创造一个christmasTree(height)方法,height代表圣诞书的高,返回值就是按圣诞树的形状和高出如christmasTree(5)就会输出如下内容
小伙看你根骨奇佳,潜力无限,来学PHP伐。
function christmasTree(height) { const col = 1 + (height - 1) * 2, char = '*', space = ' '; var space_len = height - 1, char_len = 1, tree = ''; for (let i = 0; i < height; i++) { let spaces = space.repeat(space_len); tree += `${spaces}${char.repeat(char_len)}${spaces}\n`; char_len += 2; space_len--; } console.log(tree); }
function christmasTree(x,y){ var str=y||Array.apply(null,{length:(2*x)+1}).map((m,n)=>n==x?"*":" ").join("") if(y){ str=str.replace(/\s(\*+)\s/g,"*$1*"); console.log(str) } if(x>1){christmas(x-1,str)} }
Another Tree in ES5:
function xmasTree(n) { var tree = []; for (var i=0; i < n; i++){ tree.push(Array(i+1).join(" ") + Array(2*(n-i)).join("*")); } console.log(tree.reverse().join("\n")); }
推荐一个有意思的把网站所有的js代码都压缩成圣诞树
Another Tree in ES5:
推荐一个有意思的把网站所有的js代码都压缩成圣诞树