Home > Software design >  How to print star with star pattern in JavaScript
How to print star with star pattern in JavaScript

Time:12-23

Hello i am new to js it is easy to print triangle and others shapes in js but i want to print star (★) shape the star shape pattern is so difficult.

My task is to make full star (★) shape with with loops in javascript.

like Triangle Pattern:

 let n = 5;
let string = "";
for (let i = 1; i <= n; i  ) {
  for (let j = 0; j < i; j  ) {
    string  = "*";
  }
  string  = "\n";
}
console.log(string);

https://www.tutorialstonight.com/js/javascript-star-pattern

thanks in advance.★

CodePudding user response:

let star = document.createElement("div");
star.innerHTML = '&#8902;';
document.body.appendChild(star);

CodePudding user response:

  var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    function drawStar(cx,cy,spikes,outerRadius,innerRadius){
      var rot=Math.PI/2*3;
      var x=cx;
      var y=cy;
      var step=Math.PI/spikes;

      ctx.beginPath();
      ctx.moveTo(cx,cy-outerRadius)
      for(i=0;i<spikes;i  ){
        x=cx Math.cos(rot)*outerRadius;
        y=cy Math.sin(rot)*outerRadius;
        ctx.lineTo(x,y)
        rot =step

        x=cx Math.cos(rot)*innerRadius;
        y=cy Math.sin(rot)*innerRadius;
        ctx.lineTo(x,y)
        rot =step
      }
      ctx.lineTo(cx,cy-outerRadius);
      ctx.closePath();
      ctx.lineWidth=5;
      ctx.strokeStyle='blue';
      ctx.stroke();
      ctx.fillStyle='skyblue';
      ctx.fill();
    }

    drawStar(100,100,5,30,15);
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>

CodePudding user response:

console.log(`
        .
       ,O,
      ,OOO,
'oooooOOOOOooooo'
   OOOOOOOOOOO
     OOOOOOO
    OOOO'OOOO
   OOO'   'OOO
  O'         'O
`);

  • Related