Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Looping through an array by index worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (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 | |
|---|---|---|
| 1for (let i = 0; i < scores.length; i++) | AStarting i at 1 skips the first item at index 0. The counter should start at 0. | |
| 2scores[i] = scores[i] + 5; | Bfor (let i = 0; i < scores.length; i++) { console.log(scores[i]); } | |
| 3Use the loop counter as the index to read each item and print it | CCounts i from 0 up to the last index so every item in the scores array can be visited | |
| 4Stop the loop before i reaches the array length, so no index goes past the end | Di < scores.length | |
| 5for (let i = 0; i <= scores.length; i++) | EUsing <= lets i reach scores.length, which is one step past the end, so scores[i] is undefined on the last pass. Use < instead. | |
| 6for (let i = 1; i < scores.length; i++) | FChanges the item at index i in place by adding 5 to it |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
for (let i = 0; i < scores.length; i++) — A loop that counts i from 0 up to the last index, so scores[i] visits every item in the array. Use < not <=, or i goes one step past the end and reads undefined.for (let i = 0; i < scores.length; i++) {
console.log(scores[i]);
}for (let i = 0; i < scores.length; i++) {
console.log(scores[i]);
}for (let i = 0; i < scores.length; i++) {
console.log(scores[i]);
}for (let i = 0; i < scores.length; i++) {
console.log(scores[i]);
}for (let i = 0; i < scores.length; i++) {
scores[i] += 5;
}