introduction
Welcome to our in-depth tutorial on creating a classic number guessing game! This project will guide you through building a simple yet engaging game using HTML, CSS, and JavaScript.
Whether you’re a beginner aiming to strengthen your foundational skills or an experienced developer looking for a fun exercise, this guide has something for you.

overview
In this project, you’ll create a number guessing game where the user guesses a number between 1 and 100. The game provides feedback on whether the guess is too high, too low, or correct. We’ll also include features to track the number of attempts and offer a reset option.
By the end of this tutorial, you’ll have a fully functional game and a deeper understanding of how these three core web technologies work together.
Step 1: Crafting the HTML Structure
HTML forms the backbone of our game’s structure. Below is the foundational markup for our game interface:
Number Guessing Game
Number guessing game
Try and guess a random number between 1 and 100.
You have 10 attempts to guess the right number.
Previous Guesses:
Guesses Remaining: 10
Explanation
- Container Div: Centers and contains all game elements.
- Input Field: Allows the user to enter their guess.
- Buttons: For submitting guesses and resetting the game.
- Paragraphs: To display feedback messages and the number of attempts.
Step 2: Designing with CSS
CSS is crucial for enhancing the visual appeal of our game. Here is a simple yet effective style sheet to make our game look polished:
html {
font-family: sans-serif;
}
body {
width: 50%;
max-width: 750px;
min-width: 480px;
margin: 0 auto;
}
.lastResult {
color: white;
padding: 7px;
}
.guesses {
color: white;
padding: 7px;
}
button {
background-color: purple;
color: #fff;
width: 250px;
height: 50px;
border-radius: 25px;
font-size: 30px;
border-style: none;
margin-top: 30px;
/* margin-left: 50px; */
}
#subt {
background-color: yellow;
color: #000;
width: 350px;
height: 50px;
border-radius: 25px;
font-size: 30px;
border-style: none;
margin-top: 50px;
/* margin-left: 75px; */
}
#guessField {
color: #000;
width: 550px;
height: 100px;
font-size: 30px;
border-style: none;
margin-top: 25px;
font-size: 45px;
/* margin-left: 50px; */
border: 5px solid #14727d;
text-align: center;
}
#guess {
font-size: 55px;
/* margin-left: 90px; */
margin-top: 120px;
color: #fff;
}
.guesses {
background-color: #12aab4;
}
#wrapper {
box-sizing: border-box;
text-align: center;
width: 750px;
height: 550px;
background-color: #12aab4;
color: #fff;
font-size: 25px;
}
h1 {
background-color: #7b3056;
color: #fff;
text-align: center;
width: 750px;
}
p {
font-size: 20px;
text-align: center;
}
Styling Notes
- Body and Container: Center the game and provide a clean, centered interface.
- Buttons: Styled with color and hover effects for improved user interaction.
Key Points
- Flexbox Layout: Centers the game container vertically and horizontally.
- Button Styling: Ensures buttons are visually appealing and interactive.
- Container Styling: Adds padding, border-radius, and shadow to the game container for a modern look.
Step 3: Implementing Game Logic with JavaScript
JavaScript is where the game’s functionality comes to life. Below is the script that drives our game:
javascript code
//Generate a random number between 1 and 500
let randomNumber = parseInt((Math.random()*100)+1);
const submit = document.querySelector('#subt');
const userInput = document.querySelector('#guessField');
const guessSlot = document.querySelector('.guesses');
const remaining = document.querySelector('.lastResult');
const startOver = document.querySelector('.resultParas');
const lowOrHi = document.querySelector('.lowOrHi');
const p = document.createElement('p');
let previousGuesses = [];
let numGuesses = 1;
let playGame = true;
if (playGame){
subt.addEventListener('click', function(e){
e.preventDefault();
//Grab guess from user
const guess = parseInt(userInput.value);
validateGuess(guess);
});
}
function validateGuess(guess){
if (isNaN(guess)){
alert('Please enter a valid number');
} else if (guess < 1) {
alert('Please enter a number greater than 1!');
} else if (guess > 100){
alert('Please enter a number less than 500!')
} else {
//Keep record of number of attempted guesses
previousGuesses.push(guess);
//Check to see if game is over
if (numGuesses === 11){
displayGuesses(guess);
displayMessage(`Game Over! Number was ${randomNumber}`);
endGame();
} else {
//Display previous guessed numbers
displayGuesses(guess);
//Check guess and display if wrong
checkGuess(guess);
}
}
}
function checkGuess(guess){
//Display clue if guess is too high or too low
if (guess === randomNumber){
displayMessage(`You guessed correctly!`);
endGame();
} else if (guess < randomNumber) {
displayMessage(`Too low! Try again!`);
} else if (guess > randomNumber) {
displayMessage(`Too High! Try again!`);
}
}
function displayGuesses(guess){
userInput.value = '';
guessSlot.innerHTML += `${guess} `;
numGuesses++
remaining.innerHTML = `${11 - numGuesses} `;
}
function displayMessage(message){
lowOrHi.innerHTML = `${message}
`
}
function endGame(){
//Clear user input
userInput.value = '';
//Disable user input button
userInput.setAttribute('disabled', '');
//Display Start new Game Button
p.classList.add('button');
p.innerHTML = `Start New Game
`
startOver.appendChild(p);
playGame = false;
newGame();
}
function newGame(){
const newGameButton = document.querySelector('#newGame');
newGameButton.addEventListener('click', function(){
//Pick a new random number
randomNumber = parseInt((Math.random()*100)+1);
previousGuesses = [];
numGuesses = 1;
guessSlot.innerHTML = '';
lowOrHi.innerHTML = '';
remaining.innerHTML = `${11 - numGuesses} `;
userInput.removeAttribute('disabled');
startOver.removeChild(p);
playGame = true;
})
}
//Allow to restart game with restart button
//Change DIV to a form so it can accept the enter key
//NOTES:
//NaN != NaN
JavaScript Breakdown
- startgame Function: Initializes a new game with a random number and resets attempts.
- checkguess Function: Compares the player’s guess to the random number and updates the message and attempt count.
- Event Listeners: Handle user interactions for submitting guesses and resetting the game.’
Conclusion
Congratulations! You’ve built a fully functional Number Guessing Game using HTML, CSS, and JavaScript. This project provides a solid foundation for learning how to combine these technologies to create interactive web applications.
Feel free to extend this project by adding features such as difficulty levels, a timer, or a leaderboard. The possibilities are endless!