题目,根据传入的正整数,画一颗树
![Uploading Paste_Image_548301.png . . .]
我的解答
function towerBuilder(nFloors) {
var box = [];
nFloors = nFloors*2 - 1;
while (nFloors > 0) {
box.push(nFloors);
nFloors = nFloors - 2;
}
var box2 = [];
box.forEach(function(item, index, array) {
var box3 = [];
for (i=0; i< index; i++) {box3.push(' ')};
for (j=0; j< item; j++) {box3.push('*')};
for (k=0; k< index; k++) {box3.push(' ')};
var level = box3.join('');
box2.push(level);
});
return box2.reverse();
}
别人的解答
function towerBuilder(n) {
return Array.from({length: n}, function(v, k) {
const spaces = ' '.repeat(n - k - 1);
return spaces + '*'.repeat(k + k + 1) + spaces;
});
}
我的感想
- “别人的解答”表示不是很懂
- JS居然不能像python一样 2* 'a' = 'aa',好麻烦,哈哈哈哈
网友评论