Exercise 8: Counting Boxes
<!DOCTYPE html>
<html>
<head> Big Head Count
</head>
<body>
<h1>Time to Count The Shapes</h1>
<p> Count the Red squares on the screen </p>
<fieldset>
<canvas id="myCanvas" width="1200" height="300" style="border:none;">
</canvas>
<label> How many shapes do you see? </label>
<input type="number" id="answer"> </input>
<!-- <button id="colors" onclick="colors()" > Colors </button> -->
<button id="eraser" onclick="eraser()" > Next </button>
<button id="compare" onclick="compare();eraser()" > Answer </button>
<p> <b> Score: </b> </p> <div id="myScore"> 0 </div>
<p id="demo"> </p>
</fieldset>
</body>
<script type = "text/javascript">
var score=0;
function colors()
{
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
x=Math.round(Math.random()*10+1);
for(f=1; f<=x;f++)
{
switch(f)
{
case 1: c="#ff2a00"; break
case 2: c="#ffa500"; break
case 3: c="#ffd700"; break
case 4: c="#58c142"; break
case 5: c="#2c51e1"; break
case 6: c="#0d8e91"; break
case 7: c="#cc33aa"; break
case 8: c="#ff8db7"; break
case 9: c="#ac6ab9"; break
case 10: c="#1b0f22"; break
}
ctx.font = "20px Calibri";
ctx.fillStyle=c;
ctx.fillRect(10+f*100,100, 50, 50);
}
}
function eraser()
{
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,1200,300);
colors();
}
colors()
function compare()
{
ans=document.getElementById("answer").value
if (ans==x) {
score += 1;
document.getElementById("myScore").innerHTML=score.toString()
document.getElementById("demo").innerHTML="Hooray! You are correct! "
}
else {
document.getElementById("demo").innerHTML="Sorry, you are wrong..."
}
}
</script>
Comments
Post a Comment