JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<body>
<div id="content">
<h1>Whack-A-Mole</h1>
<p>After clicking "start", you will have 30 seconds to click
as many moles as you can. The moles appear randomly so be ready! </p>
<div id="controls">
<span id="score">0 pts</span>
<button type="button" id="start_button">Start!</button>
</div>
<div id="timer">30 seconds left</div>
<div id="gamespace" style="position:relative;">
</div>
</div>
</body>
JavaScript
var score=0;
var time_left=30;
var clock;
var t;
var xPos;
var yPos;
$(document).ready(function(){
$("#start_button").click(function(){
start();
updateTimer();
});//end start_button
$("#gamespace").on("click", "img", function(){
incScore();
});//end.on
});//end.ready
function start(){
addMole();
$("h1").css("color","#005b7f");
$("#gamespace").css("backgroundColor","#cdeaf5");
};//end start
function randPosX(){
return Math.floor(Math.random()*300);
};//end x
function randPosY(){
return Math.floor(Math.random()*600);
};//end y
function addMole(){
console.log(xPos, yPos);
xPos = randPosX();
yPos = randPosY();
$("#gamespace").append('<img alt="Mole" src="img/mole.png" style="position:absolute; top:'+xPos+'px; left:'+yPos+'px;" />');
t=setTimeout(addMole, 2000);
};//end mole
function updateTimer(){
$("#timer").show();
if (time_left >0){
$("#timer").html(time_left+" seconds left");
clock=setTimeout(updateTimer, 1000);
time_left -=1;
}else{
$("#timer").html("Out of time!")
clearTimeout();
}
};//end timer
function incScore(){
score +=1;
$("#score").html(score+" points");
};//end score