创造一个christmasTree(height)方法,height代表圣诞书的高,返回值就是按圣诞树的形状和高出
如christmasTree(5)就会输出如下内容
伊谢尔伦2017-04-11 10:55:30
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);
}
高洛峰2017-04-11 10:55:30
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)}
}
ringa_lee2017-04-11 10:55:30
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"));
}