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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.