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 | |
|---|---|---|
| 1let total = 0; | Alet total = 0 is inside the loop, so the total resets to 0 on every pass. It must be declared once before the loop. | |
| 2return total / nums.length; | BSets the running total to 0 before the loop starts, so each array item can be added to it. | |
| 3A loop adds every item in the array to the running total | Ctotal + num works out a value but never stores it. It should be total += num so the total is updated. | |
| 4Check for an empty array first so you never divide by zero | Dfor (const num of nums) { total += num; } | |
| 5for (const num of nums) { let total = 0; total += num; } | EDivides the total by the number of items in the array to give the average. | |
| 6for (const num of nums) { total + num; } | Fif (nums.length === 0) return 0; |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
total += num — Adds each array item to a running total inside a loop. Set let total = 0 before the loop, then return total. Divide by nums.length for the average.function sumArray(nums) {
let total = 0;
for (const num of nums) total += num;for (const num of nums) {
total += num;
}let total = 0;
for (const n of nums) total += n;
return total / nums.length;if (nums.length === 0) {
return 0;
}const sum = sumArray(nums);
return sum / nums.length;