Home  • Programming • JavaScript

Create your own guessing game in JavaScript

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Guessing Game</title>
<script>

function startGame(){
  i=1;
  computer=Math.floor(Math.random()*100); 
  score=1000;
  
  while(true){   
	  player=parseInt(prompt("Enter your number:")); 
	  if(player>computer){
		  alert("Your number is greater than my one. You have tried "+i); 
	  }else if(player<computer){
		  alert("Your number is less than my one. You have tried "+(i)); 
	  }else if(computer==player){
		  alert("You win!!. You have taken "+i+" times. Your score is:"+(Math.floor(score/i)));
		  break;
	  }
	  	  	  
	  if(i>=10){
		  alert("Game Over! I guessed: "+computer+". Press F5 to try again.");
		  break;
	  }  
	 
		i++; 
  }

}
</script>

</head>
<body>
<button onClick="startGame()">Start Game</button>
</body>
</html>

Comments 0


Share

Copyright © 2024. Powered by Intellect Software Ltd