diff --git a/js/script.js b/js/script.js index 6449c99..6e2810c 100644 --- a/js/script.js +++ b/js/script.js @@ -9,7 +9,7 @@ const message = document.querySelector(".message"); const playAgainButton = document.querySelector(".play-again"); let word = "magnolia"; -const guessedLetters = []; +let guessedLetters = []; let remainingGuesses = 8; // API setup @@ -118,6 +118,7 @@ const updateGuessesRemaining = function (guess) { if (remainingGuesses === 0) { message.innerHTML = `Game over! The word was ${word}.`; + startOver(); } else if (remainingGuesses === 1) { remainingGuessesSpan.innerText = `${remainingGuesses} guess`; } else { @@ -130,5 +131,30 @@ const playerWinStatus = function () { if (word.toUpperCase() === wordInProgress.innerText) { message.classList.add("win"); message.innerHTML = `
You guessed the correct word! Congrats!
`; + startOver(); } }; + +//start over button +const startOver = function () { + guessButton.classList.add("hide"); + remainingGuessesElement.classList.add("hide"); + lettersGuessedElement.classList.add("hide"); + playAgainButton.classList.remove("hide"); +}; +// clear and play again +playAgainButton.addEventListener("click", function () { + message.classList.remove("win"); + guessedLetters = []; + remainingGuesses = 8; + remainingGuessesSpan.innerText = `${remainingGuesses} guesses`; + lettersGuessedElement.innerHTML = ""; + message.innerText = ""; + getWord(); + + // display play again button + guessButton.classList.remove("hide"); + playAgainButton.classList.add("hide"); + remainingGuessesElement.classList.remove("hide"); + lettersGuessedElement.classList.remove("hide"); +}); \ No newline at end of file