Difference between revisions of "Flags with Repeats"

From ProgZoo
Jump to navigation Jump to search
Line 20: Line 20:
   ctx.fillStyle = 'red';
   ctx.fillStyle = 'red';
   ctx.fillRect(0,0,250,155);
   ctx.fillRect(0,0,250,155);
   for(let i=0;i<5;i++){
   for(let i=0;i<3;i++){
    ctx.fillStyle='white';
     ctx.beginPath();
     ctx.beginPath();
     ctx.moveTo(0,0);
     ctx.moveTo(0,0);
Line 28: Line 29:
     ctx.lineTo(0,31);
     ctx.lineTo(0,31);
     ctx.fill();
     ctx.fill();
    ctx.translate(0,31);
   }
   }
}
function star(ctx)
{
  ctx.beginPath();
  [ [ 0,-50],[11,-16],[48,-16],[18,6],[30,41],
    [ 0,18],[-30,41],[-18,6],[-48,-16],[-11,-16]
  ].forEach((xy) => ctx.lineTo(xy[0],xy[1]));
  ctx.fill();
}
}
</pre>
</pre>
Line 44: Line 37:
{
{
   ctx.fillStyle = 'red';
   ctx.fillStyle = 'red';
   ctx.fillRect(0,0,200,150);
   ctx.fillRect(0,0,250,155);
   ctx.translate(100,75);
   for(let i=0;i<5;i++){
  ctx.fillStyle='yellow';
    ctx.fillStyle='white';
  star(ctx);
    ctx.beginPath();
}
    ctx.moveTo(0,0);
 
    ctx.lineTo(64,0);
function star(ctx)
    ctx.lineTo(100,15);
{
     ctx.lineTo(64,31);
  ctx.beginPath();
    ctx.lineTo(0,31);
  [ [ 0,-50],[11,-16],[48,-16],[18,6],[30,41],
    ctx.fill();
     [ 0,18],[-30,41],[-18,6],[-48,-16],[-11,-16]
    ctx.translate(0,31);
  ].forEach((xy) => ctx.lineTo(xy[0],xy[1]));
  }
  ctx.fill();
}
}
</pre>
</pre>
</div>
</div>

Revision as of 08:54, 10 August 2021

<div style='background:silver;padding:5px'>
<canvas id='usr' width=--snippet-w-- height=--snippet-h--></canvas>
<canvas id='ans' width=--snippet-w-- height=--snippet-h-- style='display:none'></canvas>
</div>
--snippet-usr--
drawFlag(document.getElementById('usr').getContext('2d'));
(()=>{
  --snippet-ans--
  drawFlag(document.getElementById('ans').getContext('2d'));
})();
let [a,b] = ['usr','ans']
    .map(id=>document.getElementById(id))
    .map(elem=>elem.getContext('2d').getImageData(0,0,elem.width,elem.height))
let diff = a.data.map((v,i) => v===b.data[i]?1:0).reduce((acc,v)=>acc+v,0)*100/a.data.length;
let fb = document.createElement('div');
fb.innerText = `Score: ${diff.toFixed(1)}`;
document.body.appendChild(fb);
fetch(`/reportProgress.php?uid=--snippet-uid--&qid=--snippet-qid--&score=${diff}`);

We can repeat a statement or block of statements with a for loop. The classic for loop takes the following form:

for(let i = 0; i< 10; i++){
   console.log(i);
}

The body of the loop runs 10 times, control variable i takes every value 0,1,2..9 in turn. We can use the value of i inside the loop if required.

Bahrain

The flag of Bahrain has a five irregular pentagons on a red background.

Each pentagon can be represented by five points such as (0,0), (64,0) (100,15) (64,31) and (0,31).

Flagbahrain.png

function drawFlag(ctx)
{
  ctx.fillStyle = 'red';
  ctx.fillRect(0,0,250,155);
  for(let i=0;i<3;i++){
    ctx.fillStyle='white';
    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(64,0);
    ctx.lineTo(100,15);
    ctx.lineTo(64,31);
    ctx.lineTo(0,31);
    ctx.fill();
    ctx.translate(0,31);
  }
}
function drawFlag(ctx)
{
  ctx.fillStyle = 'red';
  ctx.fillRect(0,0,250,155);
  for(let i=0;i<5;i++){
    ctx.fillStyle='white';
    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(64,0);
    ctx.lineTo(100,15);
    ctx.lineTo(64,31);
    ctx.lineTo(0,31);
    ctx.fill();
    ctx.translate(0,31);
  }
}