Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. output = document.getElementById("output");
  2. //Create an object to hold the game info
  3. var game = {
  4. num : 0,
  5. turns : 0,
  6. reset : function() {
  7. //function to reset
  8. this.turns = 0;
  9. this.newNum();
  10. },
  11. newNum : function() {
  12. //get a random integer between 0 and 10
  13. this.num = parseInt(Math.random() * 10) + 1;
  14. },
  15. checkGuess : function(guess) {
  16. //try to convert the guess into a integer
  17. try {
  18. guess = parseInt(guess);
  19. } catch(e) {
  20. alert("Enter a guess!");
  21. this.turns++;
  22. return false;
  23. }
  24.  
  25. //perform strict check of equality
  26. if (guess === this.num) {
  27. output.innerhtml = "Correct! It took you " + this.turns + " turn(s) to guess my number";
  28. return true;
  29. } else if (guess > this.num) {
  30. output.innerhtml = "Your guess is too high. Try again.";
  31. this.turns++;
  32. return false;
  33. } else {
  34. output.innerhtml = "Your guess is too low. Try again.";
  35. this.turns++;
  36. return false;
  37. }
  38. }
  39. };
  40.  
  41. function guessNumber() {
  42. var guess = document.getElementById("guess").value;
  43. game.checkGuess(guess);
  44. }
  45.  
  46. function resetGame() {
  47. game.reset();
  48. }
  49.  
  50. resetGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement