JSFiddle - React, Tailwind, and code Playground

by Jenny Florence

HTML

<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">

			
			
		</div>
		
	</div>
  </body>

JavaScript

<script>
		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(){
			$("#gamespace").append('<img src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
			randPosX();
			randPosY();
			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 
	</script>