JavaScript NUMBER GUESSING GAME (8:54)
Build a complete number guessing game using JavaScript. Learn to generate random numbers, validate user input, use while loops for game flow, and provide interactive feedback to players.
- Random number generation with Math.random()
- User input with window.prompt()
- Input validation (type checking and range checking)
- While loops for game control
- Conditional statements (if/else if/else)
- Type conversion (String to Number)
- Template literals for dynamic messages
- Attempt tracking
- Random number generation between min and max range
- User input validation
- "Too High" and "Too Low" hints
- Attempt counter
- Win message with total attempts
- Configurable difficulty (change min/max range)
- Game generates a random number between 1 and 100
- Player enters a guess
- Game provides feedback:
- "Too Low" if guess < answer
- "Too High" if guess > answer
- "Correct!" when guess matches answer
- Game tracks number of attempts
- Player wins when they guess correctly
const minNum = 1; // Minimum possible number
const maxNum = 100; // Maximum possible numberFrom transcript: "The first thing that we're going to need is to set the minimum and the maximum numbers in our number guessing game."
const answer = Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;How it works:
Math.random()generates 0.0 to 0.999...- Multiply by range:
(maxNum - minNum + 1) Math.floor()rounds down to whole number- Add
minNumto shift range
Formula breakdown:
// For range 1-100:
// (100 - 1 + 1) = 100 (range size)
// Math.random() * 100 = 0.0 to 99.999...
// Math.floor(...) = 0 to 99
// + 1 = 1 to 100 ✓From transcript: "We will multiply this by within a set of parentheses the range between our maximum minus our minimum then add plus one."
let attempts = 0; // Track number of guesses
let guess; // Store current guess
let running = true; // Control game loopFrom transcript: "We'll create a variable name attempts to keep track of the attempts... I will also create a Boolean variable named running and I will set this to be true."
while(running) {
// Game logic here
}From transcript: "The reason that we have the Boolean variable running is so that we can exit the game when it's over. We'll set running to equal false."
guess = Number(window.prompt(`Guess a number between ${minNum} and ${maxNum}`));Important: User input is a string by default!
From transcript: "So my user input of 50 is a string data type. We'll need to convert it to a number for comparisons."
if(isNaN(guess)){
window.alert("Please enter a valid number");
}From transcript: "There's a function to check to see if something is not a number... if you type cast some characters that are non-numeric... you'll end up with not a number."
Example: Typing "pizza" results in NaN
else if (guess < minNum || guess > maxNum) {
window.alert(`Please enter a number between ${minNum} and ${maxNum}`);
}From transcript: "Now what if somebody types in a guess that's below our minimum or above our maximum."
else {
attempts++; // Increment attempt counter
if (guess < answer) {
window.alert("Too Low! Try again!");
}
else if (guess > answer) {
window.alert("Too High! Try again!");
}
else {
window.alert(`Correct! The answer was ${answer}. It took you ${attempts} attempts.`);
running = false; // End game
}
}From transcript: "If they reach the else statement that means they have a valid number. We'll increase our attempts variable by one."
// Number Guessing Game
const minNum = 1;
const maxNum = 100;
const answer = Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
let attempts = 0;
let guess;
let running = true;
while(running){
guess = Number(window.prompt(`Guess a number between ${minNum} and ${maxNum}`));
if(isNaN(guess)){
window.alert("Please enter a valid number");
}
else if (guess < minNum || guess > maxNum) {
window.alert(`Please enter a number between ${minNum} and ${maxNum}`);
}
else{
attempts++;
if (guess < answer){
window.alert("Too Low! Try again!");
}
else if (guess > answer){
window.alert("Too High! Try again!");
}
else {
window.alert(`Correct! The answer was ${answer}. It took you ${attempts} attempts.`);
running = false;
}
}
}From the video transcript:
Guess: 50 → "Too Low! Try again!"
Guess: 75 → "Too High! Try again!"
Guess: 62 → "Too Low! Try again!"
Guess: 68 → "Too High! Try again!"
Guess: 65 → "Too High! Try again!"
Guess: 63 → "Too Low! Try again!"
Guess: 64 → "Correct! The answer was 64. It took you 7 attempts."
// Easy: 1-10
const minNum = 1;
const maxNum = 10;
// Medium: 1-50
const minNum = 1;
const maxNum = 50;
// Hard: 1-100 (default)
const minNum = 1;
const maxNum = 100;
// Expert: 50-100
const minNum = 50;
const maxNum = 100;const maxAttempts = 10;
if (attempts >= maxAttempts) {
window.alert(`Game Over! The answer was ${answer}.`);
running = false;
}if (attempts > 5) {
const difference = Math.abs(guess - answer);
if (difference <= 5) {
window.alert("Very close!");
}
}Problem: window.prompt() returns a string
Solution: Convert with Number() function
// ❌ Wrong
guess = window.prompt("Guess:");
// ✅ Correct
guess = Number(window.prompt("Guess:"));Problem: Using AND (&&) instead of OR (||)
Solution: Use OR to check if outside range
// ❌ Wrong - impossible condition
if (guess < minNum && guess > maxNum)
// ✅ Correct
if (guess < minNum || guess > maxNum)Problem: running = false in wrong place
Solution: Set to false only when correct answer
// ✅ Correct placement
else {
window.alert("Correct!");
running = false; // Inside win condition
}- Math.random() generates random decimals (0 to 1)
- Math.floor() rounds down to create integers
- Random range formula:
Math.floor(Math.random() * (max - min + 1)) + min - Type conversion needed for user input (String → Number)
- isNaN() checks if value is "Not a Number"
- While loops perfect for game loops with exit condition
- Boolean flags (
running) control loop execution - Template literals make dynamic messages easy
- Validation prevents invalid input from breaking game
- Constants - Store unchanging values (min, max, answer)
- Variables - Track changing state (attempts, guess, running)
- Random Numbers - Generate unpredictable gameplay
- Type Casting - Convert between data types
- Loops - Repeat game until win
- Conditionals - Make decisions based on input
- User Input - Interactive gameplay
- Validation - Ensure data integrity
Duration: 8:54
Difficulty: Beginner
Category: Practice Project / Game Development