From e4f8b75c83fd1d2038003e9e87676db65324c3e8 Mon Sep 17 00:00:00 2001 From: lee_c Date: Fri, 24 Nov 2023 17:12:45 -0800 Subject: [PATCH] Setup startover function, and added function calls where the game completes. --- js/script.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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