Looping through an array by index

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Looping through an array by index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop header visits every item in the array scores?
A for (let i = 0; i < scores.length; i++)
B for (let i = 0; i <= scores.length; i++)
2. What should the counter i start at to begin with the first item of an array?
A 1
B 0
3. scores has 4 items. Which is the index of the last item?
A 3
B 4
4. Which line inside the loop reads the current item?
A console.log(scores.i);
B console.log(scores[i]);
5. Which line adds 5 bonus points to every score, changing the items in place?
A scores[i] = scores[i] + 5;
B scores = scores[i] + 5;

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight 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

Definitions

for (let i = 0; i < scores.length; i++)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for (let i = 0; i < scores.______; i++) { console.log(scores[i]); }
2.
for (let i = 0; i _ scores.length; i++) { console.log(scores[i]); }
3.
for (let i = _; i < scores.length; i++) { console.log(scores[i]); }
4.
for (let i = 0; i < scores.length; i++) { console.log(scores[_]); }
5.
for (let i = 0; i < scores.length; i++) { scores[i] __ 5; }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A const pts = [4, 8, 6];
B let sum = 0;
C for (let i=0;i<pts.length;i++)
D sum += i;
E console.log(sum);
Question 2
A const pts = [4, 8, 6];
B for (let i=1;i<pts.length;i++)
C pts[i] = pts[i] + 1;
D console.log(pts);
Question 3
A const a = [3, 6, 9];
B let n = 0;
C for (let i=0;i<=a.length;i++)
D n += a[i];
E console.log(n);
Question 4
A const pts = [2, 4, 6];
B for (let i=0;i<pts.length;i++)
C console.log(pts(i));
Question 5
A const pts = {4, 8, 6};
B for (let i=0;i<pts.length;i++)
C console.log(pts[i] * 2);

Answer Key

Looping through an array by index
Section A: Quick Questions
Q1
A — for (let i = 0; i < scores.length; i++)
Q2
B — 0
Q3
A — 3
Q4
B — console.log(scores[i]);
Q5
A — scores[i] = scores[i] + 5;
Section B: Match the Pairs
Q1
for (let i = 0; i < scores.length; i++) → Counts i from 0 up to the last index so every item in the scores array can be visited
Q2
scores[i] = scores[i] + 5; → Changes the item at index i in place by adding 5 to it
Q3
Use the loop counter as the index to read each item and print it → for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q4
Stop the loop before i reaches the array length, so no index goes past the end → i < scores.length
Q5
for (let i = 0; i <= scores.length; i++) → Using <= lets i reach scores.length, which is one step past the end, so scores[i] is undefined on the last pass. Use < instead.
Q6
for (let i = 1; i < scores.length; i++) → Starting i at 1 skips the first item at index 0. The counter should start at 0.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q2
for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q3
for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q4
for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q5
for (let i = 0; i < scores.length; i++) { scores[i] += 5; }
Section E: Spot the Error
Q1
D — This adds the counter i instead of the item at that index, so the total is 0 + 1 + 2 = 3. It should be sum += pts[i]; to add each score.
Q2
B — The counter starts at 1, so index 0 is skipped and the first score is never changed. A loop over every item must start at i=0.
Q3
C — Using <= makes i reach 3, which is one step past the last index (2). a[3] is undefined, so n becomes NaN. It should be i<a.length.
Q4
C — Round brackets try to call pts as a function, which causes a TypeError. Items are read with square brackets: pts[i].
Q5
A — Curly braces are for objects and blocks, so this is a syntax error. An array uses square brackets: [4, 8, 6].