Higher or lower number guessing game

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Higher or lower number guessing game

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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;
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
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!"); }
4. Where should the guess counter go to count every guess?
A Inside the loop, with guesses++;
B Before the loop, with guesses++;
5. Which starting value for guesses is right before the first guess is made?
A let guesses = secret;
B let guesses = 0;

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1let secret = Math.floor(Math.random() * 100) + 1; Aif (guess > secret) { console.log('Too high!'); }
2while (guess !== secret) { BA single = assigns a value instead of comparing. The loop should use !== to keep going until the guess matches the secret.
3Tell the player their guess is too high by checking if it is bigger than the secret number. CKeeps repeating the loop until the player's guess equals the secret number.
4Count the number of guesses by adding 1 to a counter each time round the loop. Dguesses++;
5while (guess = secret) { EMath.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.
6let secret = Math.random(100); FPicks a random whole number from 1 to 100 and stores it as the secret number.

Definitions

while (guess !== secret)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let r = Math.random() * 100; let secret = Math._____(r) + 1;
2.
let guess = 0; while (guess ___ secret) { guess = Number(prompt("Guess?"));
3.
if (guess > secret) { console.log("Too high!"); } else __ (guess < secret) {
4.
let guesses = 0; while (guess !== secret) { guesses__;
5.
console.log(`Correct in ${_______} tries`);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let r = Math.random() * 10;
B let secret = Math.Floor(r)+1;
C let guesses = 0;
Question 2
A let secret = 7;
B let guess = 0;
C while (guess = secret) {
D guess = Number(prompt('?'));
E }
Question 3
A let r = Math.random * 10;
B let secret = Math.floor(r)+1;
C console.log(secret);
Question 4
A if (guess > secret) {
B console.log('Too low!');
C } else if (guess < secret) {
D console.log('Too low!');
E }
Question 5
A let secret = 7, guess = 0;
B let guesses = 0;
C while (guess !== secret) {
D guesses--;
E guess = Number(prompt('?'));
F }

Answer Key

Higher or lower number guessing game
Section A: Quick Questions
Q1
B — let secret = Math.floor(Math.random() * 10) + 1;
Q2
A — Keep asking while the guess is not equal to the secret
Q3
A — if (guess > secret) { console.log("Too high!"); }
Q4
A — Inside the loop, with guesses++;
Q5
B — let guesses = 0;
Section B: Match the Pairs
Q1
let secret = Math.floor(Math.random() * 100) + 1; → Picks a random whole number from 1 to 100 and stores it as the secret number.
Q2
while (guess !== secret) { → Keeps repeating the loop until the player's guess equals the secret number.
Q3
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!'); }
Q4
Count the number of guesses by adding 1 to a counter each time round the loop. → guesses++;
Q5
while (guess = secret) { → A single = assigns a value instead of comparing. The loop should use !== to keep going until the guess matches the secret.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
let r = Math.random() * 100; let secret = Math.floor(r) + 1;
Q2
let guess = 0; while (guess !== secret) { guess = Number(prompt("Guess?"));
Q3
if (guess > secret) { console.log("Too high!"); } else if (guess < secret) {
Q4
let guesses = 0; while (guess !== secret) { guesses++;
Q5
console.log(`Correct in ${guesses} tries`);
Section E: Spot the Error
Q1
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;
Q2
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) {
Q3
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.
Q4
B — This branch runs when the guess is greater than the secret, so the guess is too high. The message should be 'Too high!'
Q5
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.