Repeating until valid input is given

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

Repeating until valid input is given

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop keeps asking until the number is between 1 and 10?
A while (num < 1 || num > 10) { num = Number(prompt("1-10?")); }
B while (num >= 1 && num <= 10) { num = Number(prompt("1-10?")); }
2. Where should the first prompt() go in a validation loop that checks the answer?
A Nowhere, the loop can check num before it has a value
B Before the loop, so the condition has a value to check
3. Where must the prompt() go so the student is asked again after a bad answer?
A Only before the loop, so it runs once
B Inside the loop body, so num is updated each time round
4. Which line inside the loop gives the student an error message each time they type a bad answer?
A alert("Please enter a number from 1 to 10");
B alert = "Please enter a number from 1 to 10";
5. A student types 15 when the program asks for 1 to 10. What happens with this loop: while (num < 1 || num > 10)?
A The condition is true, so the loop runs and asks again
B The condition is false, so the program carries on

Match the Pairs

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

Left columnRight column
1while (num < 1 || num > 10) { Alet num = Number(prompt("Pick 1-10:"));
2num = Number(prompt("Pick 1-10:")); Bconsole.log("Invalid! Try again.");
3Ask once before the loop so the variable has a value for the first check CThere is no prompt() inside the loop, so num never changes. The condition stays true and the loop runs forever.
4Show an error message each time the answer fails the range check DThe condition checks for valid numbers, not invalid ones. Almost every number is >= 1 or <= 10, so the loop keeps repeating even for good answers.
5while (num >= 1 || num <= 10) { EAsks the user again inside the loop and saves the new answer, so the condition can be checked again.
6while (num < 1 || num > 10) { console.log("Out of range!"); } FKeeps looping while the number is outside the range 1 to 10, so the loop body runs only for invalid answers.

Definitions

while (!(num >= 1 && num <= 10))

Fill in the Blanks

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

1.
_____ (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
2.
while (n < 1 __ n > 10) { n = Number(prompt("Try again:")); }
3.
while (n < 1 || n > 10) { n = Number(______("Try again:")); }
4.
___________("Out of range!"); n = Number(prompt("Try again:")); }
5.
let n = ______(prompt("Pick 1-10:")); while (n < 1 || n > 10) { n = Number(prompt("Again:")); }

Spot the Error

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

Question 1
A let n = Number(prompt("N?"));
B while (n < 1 || n > 10) {
C console.log("Try again");
D n == Number(prompt("N?"));
E }
Question 2
A let a = Number(prompt("Age"));
B while (a < 1 && a > 120) {
C console.log("Bad age");
D a = Number(prompt("Age"));
E }
Question 3
A let n = Number(prompt("N?"));
B if (n < 1 || n > 10) {
C alert("Enter 1 to 10");
D n = Number(prompt("N?"));
E }
Question 4
A let p = Number(prompt("P"));
B while (p < 0 || p > 50) {
C console.log("Use 0 to 50");
D let p = Number(prompt("P"));
E }
Question 5
A let n;
B while (n < 1 || n > 10) {
C console.log("Out of range");
D n = Number(prompt("N?"));
E }

Answer Key

Repeating until valid input is given
Section A: Quick Questions
Q1
A — while (num < 1 || num > 10) { num = Number(prompt("1-10?")); }
Q2
B — Before the loop, so the condition has a value to check
Q3
B — Inside the loop body, so num is updated each time round
Q4
A — alert("Please enter a number from 1 to 10");
Q5
A — The condition is true, so the loop runs and asks again
Section B: Match the Pairs
Q1
while (num < 1 || num > 10) { → Keeps looping while the number is outside the range 1 to 10, so the loop body runs only for invalid answers.
Q2
num = Number(prompt("Pick 1-10:")); → Asks the user again inside the loop and saves the new answer, so the condition can be checked again.
Q3
Ask once before the loop so the variable has a value for the first check → let num = Number(prompt("Pick 1-10:"));
Q4
Show an error message each time the answer fails the range check → console.log("Invalid! Try again.");
Q5
while (num >= 1 || num <= 10) { → The condition checks for valid numbers, not invalid ones. Almost every number is >= 1 or <= 10, so the loop keeps repeating even for good answers.
Q6
while (num < 1 || num > 10) { console.log("Out of range!"); } → There is no prompt() inside the loop, so num never changes. The condition stays true and the loop runs forever.
Section C: Definitions
Q1
while (!(num >= 1 && num <= 10)) — A loop that re-asks with prompt() while the answer fails the check, showing an error message each time, so bad input never continues. Here it repeats while num is outside 1 to 10.
Section D: Fill in the Blanks
Q1
while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Q2
while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Q3
while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Q4
console.log("Out of range!"); n = Number(prompt("Try again:")); }
Q5
let n = Number(prompt("Pick 1-10:")); while (n < 1 || n > 10) { n = Number(prompt("Again:")); }
Section E: Spot the Error
Q1
D — == compares instead of assigning, so the new answer is never saved in n. The condition stays true and the loop never ends. It should be n = Number(prompt("N?"));
Q2
B — No number is below 1 and above 120 at the same time, so && is never true. The loop is skipped and bad ages get through. It should use || to catch either bad case.
Q3
B — An if only checks once, so the second answer might still be bad. It should be while so the program keeps asking until the answer is valid.
Q4
D — let makes a new p that only exists inside the loop. The p in the while condition never changes, so the loop never ends. It should be p = Number(prompt("P"));
Q5
A — The program must ask once before the loop. Without it n is undefined, the comparisons are false and the loop is skipped. It should be let n = Number(prompt("N?"));