Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Combining sequence, selection and iteration in one program 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 count = 0; for (let i = 1; i <= 10; i++) { if (i % 2 === 0) { count++; } } | AThe total is set up after the loop. It must be created and set to 0 before the loop, otherwise the loop uses a variable that does not exist yet and gives an error. | |
| 2console.log("Total: " + total); | BPrints the final total after the loop has finished, so it only shows the finished answer once | |
| 3Set a total to 0 before the loop so it has a starting value to add to | Clet total = 0; while (total < 20) { total += 5; } | |
| 4An if inside a for loop makes a decision on every repeat, and the result is printed after the loop | Dlet big = 0; for (let n = 1; n <= 5; n++) { if (n > 3) { big++; } } console.log(big); | |
| 5for (let i = 1; i <= 5; i++) { total += i; } let total = 0; | ESets a counter to 0 first, loops through the numbers 1 to 10, and uses an if inside the loop to count only the even numbers | |
| 6let count = 0; for (let i = 1; i <= 5; i++) { count++; console.log(count); } | FThe console.log is inside the loop, so it prints five times instead of printing the final result once. It should go after the closing brace. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
sequence, selection, iteration — A complete program sets up variables first, repeats with a loop, uses an if inside the loop to make decisions, then prints the result after the loop ends.let total = 0;
for (let i = 1; i <= 5; i++) { total += i; }
console.log(total);for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) { evens++; }
}let total = 0;
for (let i = 1; i <= 3; i++) { total += i; }
console.log(total);let n = 1;
let big = 0;
while (n <= 5) { if (n > 3) big++; n++; }let count = 0;
for (let i = 1; i <= 8; i++) { count++; }
console.log(count);