0

How to draw such a triangle in JavaScript?

      *
     ***
    *****
   *******
  *********
 ***********
*************

It would be great if you could explain how it works.

So far I have this code:

let lines = 7;
let str = ' ';
for (let i = 0; i < lines; i++) {
  str += '*';
  console.log(str);
}
  • 1
    [Similar](https://codegolf.stackexchange.com/a/99310/21336). Don't want the trunk? Change the final `l` to `""` – James Aug 16 '21 at 12:59
  • Does this answer your question? [Making a star triangle using javascript function recursively](https://stackoverflow.com/questions/39677782/making-a-star-triangle-using-javascript-function-recursively) – Dhana D. Aug 16 '21 at 13:53

2 Answers2

0

This code should work:

// You can specify line number
function drawTriangle(lines = 7) {
  for (let i = 0; i < lines * 2; i+=2) {
    const str = " ".repeat((lines * 2 - i) / 2) // Put spaces before '*'
                  + "*".repeat(i + 1);          // Put '*' after spaces
    console.log(str);
  }
}

drawTriangle();  // 7 lines
drawTriangle(9);
drawTriangle(20);
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
0

Draw a spruce forest in ASCII-art style

First specify the dimensions of the picture, and then specify the coordinates of the vertices of the spruce trees.

Spruce forest:

      *                *                                                        
     ***       *      ***        *                                        *     
    *****     ***    *****      ***         *                            ***    
   *******   *****  *******    *****       ***                  *       *****   
  ********* ****************  *******     *****        *       ***     *******  
 *************************************   *******      ***     *****   ********* 
*************************************** *********    *****   ******* ***********
**************************************************  ******* ********************
********************************************************************************

Try it online!

function spruceForest(width, height) {
  let forest = {
    plot: [],
    addSpruce: function(x, y) {
      for (let i = 0; i < height - y; i++)
        for (let j = -i; j <= i; j++)
          if (x + j >= 0 && x + j < width)
            this.plot[y + i][x + j] = 1;
      return this;
    },
    output: function() {
      for (let i = 0; i < height; i++) {
        let row = this.plot[i];
        let line = '';
        for (let j = 0; j < width; j++)
          line += row[j] == 1 ? '*' : ' ';
        console.log(line);
      }
    }
  }; // populate an empty plot
  for (let i = 0; i < height; i++) {
    forest.plot[i] = [];
    for (let j = 0; j < width; j++)
      forest.plot[i][j] = '';
  }
  return forest;
}

spruceForest(80, 9) // draw a spruce forest in ASCII-art style
  .addSpruce(6, 0).addSpruce(15, 1).addSpruce(23, 0)
  .addSpruce(33, 1).addSpruce(44, 2).addSpruce(55, 4)
  .addSpruce(64, 3).addSpruce(74, 1).output();

Inspired by this answer: Draw an ASCII spruce forest in Java