-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.js
More file actions
1 lines (1 loc) · 3.35 KB
/
hangman.js
File metadata and controls
1 lines (1 loc) · 3.35 KB
1
javascript:(function(){ const words = [ "pizza", "burger", "sushi", "taco", "pasta", "salad", "ramen", "curry", "noodle", "sandwich", "kebab", "chicken", "steak", "hotdog", "popcorn", "fries", "potato", "carrot", "broccoli", "spinach", "tomato", "cucumber", "avocado", "banana", "orange", "apple", "pear", "watermelon", "strawberry", "blueberry", "raspberry", "grape", "pineapple", "peach", "mango", "kiwi", "grapefruit", "lemon", "lime", "pomegranate", "pepperoni", "bacon", "sausage", "pancake", "waffle", "fajita", "enchilada", "quesadilla", "guacamole", "hummus", "salsa", "yogurt", "cheese", "icecream", "chocolate", "cookie", "cake", "brownie", "pudding", "jelly", "popcorn", "candy", "hot sauce", "ketchup", "mayonnaise", "mustard", "ranch dressing", "barbecue sauce", "honey mustard", "caesar dressing", "italian dressing", "tartar sauce", "syrup", "honey", "peanut butter", "jelly", "jam", "marshmallow", "caramel", "butterscotch", "whipped cream", "cherry", "apple juice", "orange juice", "cranberry juice", "pineapple juice", "grape juice", "lemonade", "iced tea", "milk", "chocolate milk", "smoothie", "coffee", "tea"]; const word = words[Math.floor(Math.random() * words.length)]; let guesses = []; let remainingGuesses = 10; let output = ""; for (let i = 0; i < word.length; i++) { output += "_ "; } const guessedLetters = document.createElement("p"); guessedLetters.textContent = "Guessed letters: "; const guessesLeft = document.createElement("p"); guessesLeft.textContent = `Guesses left: ${remainingGuesses}`; document.body.appendChild(guessedLetters); document.body.appendChild(document.createElement("br")); document.body.appendChild(guessesLeft); const wordToGuess = document.createElement("h2"); wordToGuess.textContent = output; document.body.appendChild(wordToGuess); while (remainingGuesses > 0) { let guess = prompt(`Guess a letter. ${output}\n${guessedLetters.textContent}\n${guessesLeft.textContent}`); if (!guess) { break; } else if (guess.length !== 1) { alert("Please enter a single letter."); } else if (guesses.includes(guess)) { alert(`You already guessed "${guess}". Please try a different letter.`); } else if (word.includes(guess)) { guesses.push(guess); let newOutput = ""; for (let i = 0; i < word.length; i++) { if (guesses.includes(word[i])) { newOutput += word[i] + " "; } else { newOutput += "_ "; } } output = newOutput; wordToGuess.textContent = output; guessedLetters.textContent += `${guess} `; if (!output.includes("_")) { alert(`Congratulations! You won! The word was ${word}. You guessed it in ${guesses.length} attempts.`); break; } } else { remainingGuesses--; guessesLeft.textContent = `Guesses left: ${remainingGuesses}`; guessedLetters.textContent += `${guess} `; if (remainingGuesses === 0) { alert(`Game over. The word was "${word}".`);}}}})();