Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Repeating until valid input is given 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 | |
|---|---|---|
| 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. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.while (n < 1 || n > 10) {
n = Number(prompt("Try again:"));
}while (n < 1 || n > 10) {
n = Number(prompt("Try again:"));
}while (n < 1 || n > 10) {
n = Number(prompt("Try again:"));
} console.log("Out of range!");
n = Number(prompt("Try again:"));
}let n = Number(prompt("Pick 1-10:"));
while (n < 1 || n > 10) {
n = Number(prompt("Again:")); }