const guessInput = document.getElementById('guess');
const message = document.getElementById('message');
const guess = document.querySelector('.checkGuess').addEventListener('click',updateGame)
const showNum = document.querySelector('.showNumber').addEventListener('click',showNumber)
const exit = document.querySelector('.exitGame').addEventListener('click',exitGame)
const MINIMUM_NUMBER_TO_GUESS = 100
const MAXIMUM_NUMBER_TO_GUESS = 1000
let numberToGuess;
let numberOfGuessesDone;
let gameOver;
function generateRandomIntBetweenTwoNumbers(minNumber, maxNumber) {
return Math.floor(Math.random() * (maxNumber - minNumber + 1) + minNumber)
}
const resetGame = (minNumber, maxNumber) => {
numberToGuess = generateRandomIntBetweenTwoNumbers(minNumber, maxNumber);
numberOfGuessesDone = 0;
gameOver = false;
message.textContent = "";
}
const getGuess = () => parseInt(guessInput.value)
const isGuessValid = (guess) => isNaN(guess) || guess < MINIMUM_NUMBER_TO_GUESS || guess > MAXIMUM_NUMBER_TO_GUESS
const guessedCorrectly = (guess, numberToGuess) => guess === numberToGuess
function updateGame() {
if (gameOver) return
numberOfGuessesDone++;
const guess = getGuess();
if (isGuessValid(guess)) {
message.textContent = "Please enter a valid number.";
return;
}
if (guessedCorrectly(guess, numberToGuess)) {
message.textContent = Congratulations! You guessed the number in ${numberOfGuessesDone} ${ numberOfGuessesDone === 1 ? "guess" : "guesses" }!
;
gameOver = true;
playAgainButton.style.display = "block";
guessInput.value = "";
return
}
if (guess < numberToGuess) {
message.textContent = "Too low! Guess again.";
guessInput.value = "";
return
}
message.textContent = "Too high! Guess again.";
guessInput.value = "";
}
function showNumber() {
message.textContent = The number was ${numberToGuess}.
;
}
function exitGame() {
window.close();
}
resetGame(MINIMUM_NUMBER_TO_GUESS, MAXIMUM_NUMBER_TO_GUESS);