Giter Site home page Giter Site logo

guess_number_game's Introduction

Guessing Game

This is a simple guessing game created using JavaScript, HTML, and CSS.

How to Play

  • Enter a number between 1 and 100 in the input field.
  • Click the "Guess" button to check your guess.
  • If your guess is correct, you win the game!
  • If your guess is incorrect, you can try again.
  • Click the "Show Number" button to reveal the random number.
  • Click the "Exit Game" button to exit the game.

Installation

  • Clone or download the repository.
  • Open the index.html file in your web browser.

Technologies Used

  • JavaScript
  • HTML
  • CSS

demo

guess_number_game's People

Contributors

veronicataucci avatar

Watchers

 avatar

guess_number_game's Issues

refactored code

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);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.