← ACARA Digital Technologies Years 7–8

Higher or lower number guessing game

The heart of a guessing game: keep asking for guesses until the guess equals the random secret number, using if/else if for too high or too low and a counter for the number of guesses.

Understanding Higher or lower number guessing game in ACARA Digital Technologies Years 7–8

What it does: Using > compares the guess with the secret. If the guess is bigger, the game prints a too high message. The ++ shorthand adds 1 to the guesses variable each time the loop runs, so it stores how many guesses the player has made. Math.random() gives a decimal from 0 up to (but not including) 1. Multiply by 10, use Math.floor() to round down, then add 1 to get 1 to 10. No guesses have been made yet, so the counter starts at 0. It is not related to the secret number. prompt() gives back text. Wrap it in Number() so you can compare it with the secret number.

Common mistakes: JavaScript is case sensitive. The method is Math.floor with a lowercase f, so Math.Floor causes an error. The line should be let secret = Math.floor(r)+1;. A single = assigns a value instead of comparing. The loop must keep going while the guess is NOT equal to the secret, so use while (guess !== secret) {.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 635–666) and word search (Modules 635–695).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line creates a secret whole number from 1 to 10?
let secret = Math.floor(Math.random() * 10) + 1;let secret = Math.random(10);
Matches
while (guess = secret) {
↓
A single = assigns a value instead of comparing. The loop should use !== to keep going until the guess matches the secret.
Applying
Meanings
Term
while (guess !== secret)
Meaning
The heart of a guessing game: keep asking for guesses until the guess equals the random secret number, using if/else if for too high or too low and a counter for the number of guesses.
Practice
let secret = Math.floor(Math.random() * 100) + 1;
Testing
Gaps
let secret = Math.(r) + 1;
Check Answer
Spots
1let r = Math.random() * 10;
2let secret = Math.Floor(r)+1;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Zebra' badge — Pattern Matcher, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line creates a secret whole number from 1 to 10?
A. let secret = Math.random(10);
B. let secret = Math.floor(Math.random() * 10) + 1; ✓
Math.random() gives a decimal from 0 up to (but not including) 1. Multiply by 10, use Math.floor() to round down, then add 1 to get 1 to 10.
2. What should the guessing game loop do?
A. Keep asking while the guess is not equal to the secret ✓
B. Ask exactly once, then stop
The loop repeats until the player guesses correctly, so the condition is guess !== secret.
3. The secret is 7 and the player guesses 9. Which branch runs?
A. if (guess > secret) { console.log("Too high!"); } ✓
B. else if (guess < secret) { console.log("Too low!"); }
9 is bigger than 7, so the guess is too high. The 'too low' branch only runs when the guess is smaller than the secret.
4. Where should the guess counter go to count every guess?
A. Inside the loop, with guesses++; ✓
B. Before the loop, with guesses++;
Code inside the loop runs once per guess. Code before the loop runs only once, so it would never count the extra guesses.
5. Which starting value for guesses is right before the first guess is made?
A. let guesses = secret;
B. let guesses = 0; ✓
No guesses have been made yet, so the counter starts at 0. It is not related to the secret number.

Match the Pairs

let secret = Math.floor(Math.random() * 100) + 1; → Picks a random whole number from 1 to 100 and stores it as the secret number.
while (guess !== secret) { → Keeps repeating the loop until the player's guess equals the secret number.
Tell the player their guess is too high by checking if it is bigger than the secret number. → if (guess > secret) { console.log('Too high!'); }
Count the number of guesses by adding 1 to a counter each time round the loop. → guesses++;
while (guess = secret) { → A single = assigns a value instead of comparing. The loop should use !== to keep going until the guess matches the secret.
let secret = Math.random(100); → Math.random() does not take a maximum and only gives a decimal between 0 and 1. It needs Math.floor, multiplying and adding 1 to give a whole number from 1 to 100.

Key Term

while (guess !== secret)
The heart of a guessing game: keep asking for guesses until the guess equals the random secret number, using if/else if for too high or too low and a counter for the number of guesses.

Fill in the Blanks

1. let r = Math.random() * 100; let secret = Math.floor(r) + 1;
Hint: Type the Math method that rounds a decimal down to a whole number.
2. let guess = 0; while (guess !== secret) { guess = Number(prompt("Guess?"));
Hint: Type the operator that means not strictly equal, so the loop keeps going until the guess is right.
3. if (guess > secret) { console.log("Too high!"); } else if (guess < secret) {
Hint: Type the keyword that chains a second test onto the first one.
4. let guesses = 0; while (guess !== secret) { guesses++;
Hint: Type the shorthand that adds one to the guess counter each time round the loop.
5. console.log(`Correct in ${guesses} tries`);
Hint: Type the variable name that holds how many guesses the player made.

Spot the Error

Question 1
A let r = Math.random() * 10;
B let secret = Math.Floor(r)+1;
C let guesses = 0;
Line B — JavaScript is case sensitive. The method is Math.floor with a lowercase f, so Math.Floor causes an error. The line should be let secret = Math.floor(r)+1;
Question 2
A let secret = 7;
B let guess = 0;
C while (guess = secret) {
D guess = Number(prompt('?'));
E }
Line C — A single = assigns a value instead of comparing. The loop must keep going while the guess is NOT equal to the secret, so use while (guess !== secret) {
Question 3
A let r = Math.random * 10;
B let secret = Math.floor(r)+1;
C console.log(secret);
Line A — Math.random is a function, so it needs brackets to run. Without () you multiply the function itself, not a random number. It should be Math.random() * 10.
Question 4
A if (guess > secret) {
B console.log('Too low!');
C } else if (guess < secret) {
D console.log('Too low!');
E }
Line B — This branch runs when the guess is greater than the secret, so the guess is too high. The message should be 'Too high!'
Question 5
A let secret = 7, guess = 0;
B let guesses = 0;
C while (guess !== secret) {
D guesses--;
E guess = Number(prompt('?'));
F }
Line D — The counter must go up by one for each guess, so use guesses++. Using -- would count downwards and give the wrong number of guesses.

Student with a class code? Join your class →