Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hangman!</title>
  5. </head>
  6. <body>
  7. <h1>Hangman!</h1>
  8. <script>
  9. // Create an array of words
  10. var words = [
  11. "javascript",
  12. "monkey",
  13. "amazing",
  14. "pancake"
  15. ];
  16. // Pick a random word
  17. var word = words[Math.floor(Math.random() * words.length)];
  18. // Set up the answer array
  19. var answerArray = [];
  20. for (var i = 0; i < word.length; i++) {
  21. answerArray[i] = "_";
  22. }
  23. var remainingLetters = word.length;
  24. // The game loop
  25. while (remainingLetters > 0) {
  26. // Show the player their progress
  27. alert(answerArray.join(" "));
  28. // Get a guess from the player
  29. var guess = prompt("Guess a letter, or click Cancel to stop 
  30. playing.");
  31. if (guess === null) {
  32. // Exit the game loop
  33. break;
  34. } else if (guess.length !== 1) {
  35. alert("Please enter a single letter.");
  36. } else {
  37. // Update the game state with the guess
  38. for (var j = 0; j < word.length; j++) {
  39. if (word[j] === guess) {
  40. answerArray[j] = guess;
  41. remainingLetters--;
  42. }
  43. }
  44. }
  45. // The end of the game loop
  46. }
  47. // Show the answer and congratulate the player
  48. alert(answerArray.join(" "));
  49. alert("Good job! The answer was " + word);
  50. </script>
  51. </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement