Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
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 | |
|---|---|---|
| 1for (let i = 1; i <= 10; i++) { | Afor (let i = 1; i <= 10; i++) { console.log(i); } | |
| 2while (answer !== "blue") { | BA for loop is a poor fit here because the number of repeats is unknown and the counter i is never used. A while loop (while (guess !== "7")) is clearer. | |
| 3Any for loop can be rewritten as a while loop by moving the setup before the loop and the update inside it. | CA while loop is a good choice here because we repeat until a condition changes, and we do not know how many tries it will take. | |
| 4Use a for loop for the readable choice when printing the numbers 1 to 10, because the counting is all in one place. | Dlet i = 1; while (i <= 10) { console.log(i); i++; } | |
| 5let guess = prompt("Number?"); for (let i = 0; guess !== "7"; ) { guess = prompt("Number?"); } | EThe loop never updates i, so it runs forever. Add i++ inside the loop, or use a for loop for a known count of 10. | |
| 6let i = 1; while (i <= 10) { console.log(i); } | FA for loop is a good choice here because we know exactly how many times to repeat: 10 times. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
for vs while — Use for when you know how many times to repeat, like 1 to 10. Use while when repeating depends on a condition, like asking until the answer is correct.for (let i = 1; i <= 10; i++) {
console.log(i);
}let ans = "";
while (ans !== "yes") { ans = prompt("?"); }let i = 1;
while (i <= 10) { console.log(i); i++; }for (let i = 1; i <= 5; i++) {
console.log("Hi");
}let total = 0;
for (let n = 1; n <= 5; n++) { total += n; }