JavaScript ES2024 ·
✓ verified by execution on 2026-08-14
Building your first interactive program is an exciting milestone! In this project, we are going to build a classic Number Guessing Game. By combining several foundational concepts—variables, conditionals, loops, functions, and user input—you will see how individual pieces of syntax come together to form a complete software application.
Before writing code, it is crucial to understand the high-level architecture of our game. Software engineering is largely about breaking a large problem down into smaller, solvable steps.
The game loop: generate the secret number once, then repeatedly ask for a guess and test it, leaving the loop only when the guess is correct.
Our architecture outlines three major phases. We must generate a secret answer, enter a loop that continually asks the user for their guess, and check whether that guess is the winner. Let’s break down each technical requirement.
Getting a Random Number
First, the game needs a secret number. While some languages offer a native integer generator, JavaScript provides a single uniform random generator: Math.random(). This method returns a floating-point (decimal) number between 0 (inclusive) and 1 (exclusive).
Why a decimal instead of a whole number? Because returning a uniform decimal between 0 and 1 allows developers to scale the randomness to any arbitrary range they need.
To make this a whole number between 1 and 10, we simply scale it up by multiplying by 10, and chop off the remaining decimals using Math.floor(). Adding 1 shifts our range from 0–9 up to 1–10.
javascript
console.log(Math.floor(Math.random() * 10) < 10);
Output
true
Your output
Parsing User Input
When a user types a guess into a web prompt, it usually comes back as a string, like '5'. Even though it looks like a number to human eyes, JavaScript sees it as a text character. To compare it properly against our generated secret integer, we must parse the string into a real number.
The parseInt() function is designed exactly for this. Many beginners assume that providing text containing letters will crash the program. However, parseInt() evaluates characters from left to right, stopping as soon as it encounters a non-numeric character. This is why parsing values like '10px' successfully extracts the number 10 before ignoring the letters.
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
console.log(parseInt('42px', 10));
Output
42
You predicted
The Game Loop
We need to keep asking the user for a guess until they get it right. Why use a while loop instead of a for loop? A for loop is ideal when you know the exact iteration count in advance (like iterating through a fixed array). However, in a guessing game, we don’t know how many guesses the user will need! A while loop runs indefinitely, re-evaluating its condition on every pass until the condition is met.
javascript · visualize
let i = 0;while(i < 3) { i++;}console.log(i);
3
Checking the Guess
Inside our loop, we use an if statement to evaluate whether the user’s parsed guess strictly matches the secret target. If it does, we break out of the loop and declare a victory.