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 = 0; i < 5; i++) | ARuns 6 times, with i going 0 to 5, which is one pass too many if you wanted 5 | |
| 2for (let i = 0; i <= 5; i++) | BStarting at 0 with <= 3 runs 4 times, not 3. Change <= to < to get 3 passes. | |
| 3To repeat 5 times when counting from 1, use <= 5 so the last pass is when i is 5 | Cfor (let i = 1; i < 5; i++) | |
| 4Starting at 1 but using < 5 stops early and runs only 4 times, one pass too few | DThe loop stops when i is 9, so 10 is never added. Change < to <= to include 10. | |
| 5for (let i = 0; i <= 3; i++) { console.log("Hi"); } // want 3 greetings | ERuns exactly 5 times, with i going 0, 1, 2, 3, 4 | |
| 6for (let i = 1; i < 10; i++) { total += i; } // want to add 1 to 10 | Ffor (let i = 1; i <= 5; i++) |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
off-by-one error — A loop bug where it runs one pass too many or too few, caused by a wrong start value or mixing up < and <=. For example, i starting at 0 with i <= 5 runs 6 times, not 5.for (let i = 0; i < 5; i++) {
console.log(i);
}for (let i = 1; i <= 5; i++) {
console.log(i);
}// Run 3 times: 1, 2, 3
for (let i = 1; i <= 3; i++) {
}let passes = 0;
for (let i = 0; i < 4; i++) { passes++; }
console.log(passes);// Run 7 times, starting at 0
for (let i = 0; i < 7; i++) {
}