Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Higher or lower number guessing game worksheet is part of the ACARA Digital Technologies Years 7–8 — Programming with Control Structures and Functions (JavaScript) journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.let r = Math.random() * 100;
let secret = Math.floor(r) + 1;let guess = 0;
while (guess !== secret) {
guess = Number(prompt("Guess?"));if (guess > secret) {
console.log("Too high!");
} else if (guess < secret) {let guesses = 0;
while (guess !== secret) {
guesses++;console.log(`Correct in ${guesses} tries`);