Changed around line 1
+ const words = [
+ 'javascript', 'programming', 'algorithm',
+ 'developer', 'interface', 'database',
+ 'function', 'variable', 'iteration',
+ 'framework', 'responsive', 'asynchronous'
+ ];
+
+ let selectedWord = '';
+ let guessedLetters = [];
+ let remainingGuesses = 6;
+ let gameActive = true;
+
+ const wordDisplay = document.getElementById('word-display');
+ const keyboard = document.getElementById('keyboard');
+ const guessesLeft = document.getElementById('guesses-left');
+ const gameMessage = document.getElementById('game-message');
+ const resetButton = document.getElementById('reset-button');
+ const hangmanParts = document.querySelectorAll('.hangman-part');
+
+ function initializeGame() {
+ selectedWord = words[Math.floor(Math.random() * words.length)];
+ guessedLetters = [];
+ remainingGuesses = 6;
+ gameActive = true;
+
+ // Reset display
+ wordDisplay.textContent = '_ '.repeat(selectedWord.length);
+ guessesLeft.textContent = remainingGuesses;
+ gameMessage.textContent = '';
+
+ // Reset keyboard
+ keyboard.innerHTML = 'abcdefghijklmnopqrstuvwxyz'
+ .split('')
+ .map(letter => `${letter} `)
+ .join('');
+
+ // Reset hangman
+ hangmanParts.forEach(part => part.style.display = 'none');
+
+ // Add event listeners
+ keyboard.addEventListener('click', handleLetterClick);
+ resetButton.addEventListener('click', initializeGame);
+ }
+
+ function handleLetterClick(e) {
+ if (!gameActive || !e.target.matches('button')) return;
+
+ const letter = e.target.textContent.toLowerCase();
+ e.target.disabled = true;
+
+ if (selectedWord.includes(letter)) {
+ guessedLetters.push(letter);
+ updateWordDisplay();
+ checkWin();
+ } else {
+ remainingGuesses--;
+ updateHangman();
+ checkLose();
+ }
+
+ guessesLeft.textContent = remainingGuesses;
+ }
+
+ function updateWordDisplay() {
+ wordDisplay.textContent = selectedWord
+ .split('')
+ .map(letter => guessedLetters.includes(letter) ? letter : '_')
+ .join(' ');
+ }
+
+ function updateHangman() {
+ const partIndex = 6 - remainingGuesses - 1;
+ if (partIndex >= 0 && partIndex < hangmanParts.length) {
+ hangmanParts[partIndex].style.display = 'block';
+ }
+ }
+
+ function checkWin() {
+ if (selectedWord.split('').every(letter => guessedLetters.includes(letter))) {
+ gameMessage.textContent = 'Congratulations! You won!';
+ gameActive = false;
+ }
+ }
+
+ function checkLose() {
+ if (remainingGuesses === 0) {
+ gameMessage.textContent = `Game over! The word was "${selectedWord}".`;
+ gameActive = false;
+ }
+ }
+
+ // Initialize game on page load
+ initializeGame();