最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Christmas tree in Javascript using stars - Stack Overflow

programmeradmin1浏览0评论

I have this loop problem, I really don't understand why my code doesn't work, I've even draw this on paper and for my logic it looks good but it's not working, please help me.

function drawTree(h) {
  for(var i=0; i<=h; i++){
    var star = '';
    for(var k=0; k<=1; k++){
      star += "  ";
    };
    for(var j=0; j<=i; j++) {
        star += "*";
    };
  };
    console.log(star);
};

drawTree(5);

I have this loop problem, I really don't understand why my code doesn't work, I've even draw this on paper and for my logic it looks good but it's not working, please help me.

function drawTree(h) {
  for(var i=0; i<=h; i++){
    var star = '';
    for(var k=0; k<=1; k++){
      star += "  ";
    };
    for(var j=0; j<=i; j++) {
        star += "*";
    };
  };
    console.log(star);
};

drawTree(5);

Share Improve this question asked Apr 17, 2018 at 10:22 zbierekzbierek 51 silver badge5 bronze badges 3
  • 1 You only print one line every time, the console.log is outside your loop. – Jerodev Commented Apr 17, 2018 at 10:25
  • For starters you only call console.log once but i dont see any newlines. – ASDFGerte Commented Apr 17, 2018 at 10:25
  • what was the expected result, exactly? If you're looking for a christmas tree, as the title suggests, then you'd need to start new lines in suitable places – ADyson Commented Apr 17, 2018 at 10:25
Add a ment  | 

6 Answers 6

Reset to default 5

See ments in the code for changes.

function drawTree(h) {
  for(var i=0; i<=h; i++){
    var star = '';
    //Changed to start high then decrease
    for(var k = 1; k <= h - i; k++){
      //shortened to one space
      star += " ";
    };
    for(var j=0; j<=i; j++) {
        //Added space so there is an odd number
        //of symbols and the star above fits
        //the space
        star += " *";
    };
    //Moved into the loop
    console.log(star);
  };
};

drawTree(5);

Note that the code can be substantially shortened using String.prototype.repeat:

function drawTree(h) {
  for (var i = 0; i <= h; i++){
    console.log(" ".repeat(h - i) + " *".repeat(i + 1));
  };
};

drawTree(5);

Also note that your example produces a base line with six stars for a call of drawTree(5). I am unsure whether that is intended. The code above reproduces said behavior, editing it to show a line less should not be too hard.

function drawTree(h) {
  for (var i = 0; i < h; i++) {
    var star = '';
    var space = (h - i);
    if (i == 0) {
      star += ' '.repeat(space + 1) + '\n';
    }
    star += ' '.repeat(space + 1);
    var zero = 2 * i + 1;
    star += '*'.repeat(zero);
    console.log(star);
  }
}
drawTree(5);

You’re re-setting it each line, but printing it only at the end.

Move console.log(star); to the end of the first loop.

Just for fun:

const tree = ({height: h = 5, stumpHeight: sh = 2, branchChar: bc = '*', emptyChar: ec = ' ', stumpChar: sc = '#'} = {}) => [
  ... Array .from ({length: h}, (_, n) => ec .repeat (h - n - 1) + bc.repeat (2 * n + 1) + ec .repeat (h - n - 1)),
  ... Array .from ({length: sh}, () => ec .repeat (h - 1) + sc + ec .repeat (h - 1)),
] .join ('\n')


console .log (tree ())
console .log (tree ({height: 6, emptyChar: '_'}))
console .log (tree ({height: 12, stumpHeight: 3, stumpChar: '@'}))
console .log (tree ({branchChar: '#'}))
.as-console-wrapper {max-height: 100% !important; top: 0}

Because, you know, that's how all those JS 101 instructors expect it to be coded.

function drawTree(n) {
      let tag = "#";               
      for (let i = 0, j = "#"; i < n; i++) {
        console.log(tag);
        tag = tag + j;
      }
    }

drawTree(10);

You have to place the console.log statement inside first for loop. That should give a half tree. If you want correct tree, then pre-pend spaces before star. Calculate the center of the number of rows and add spaces before it.

Hope this not an assignment problem.

function drawTree(h) {
  for(var i=0; i<=h; i++){
    var star = '';
    for(var k=0; k<=1; k++){
      star += "  ";
    };
    for(var j=0; j<=i; j++) {
        star += "*";
    };
    console.log(star);
  };

};

drawTree(5);

发布评论

评论列表(0)

  1. 暂无评论