Reading beyond the end of an array

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

Reading beyond the end of an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. let scores = [5, 8, 9, 7, 6, 10, 4, 3, 8, 9]; What does console.log(scores[10]) show?
A 9
B undefined
2. What happens when you read an index that does not exist, like colours[5] in a 3-item array?
A It gives undefined and the program keeps running
B It throws an error and the program stops
3. Which expression gets the LAST item of an array called names?
A names[names.length - 1]
B names[names.length]
4. let pets = ['cat', 'dog', 'fish']; What does console.log(pets[3]) show?
A undefined
B fish
5. let pets = ['cat', 'dog', 'fish']; What does console.log(pets[-1]) show?
A fish
B undefined

Match the Pairs

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

Left columnRight column
1let scores = [5, 8, 9]; console.log(scores[3]); AThe loop uses <= so the last pass reads scores[scores.length], which is undefined. It should use i < scores.length.
2console.log(scores[-1]); Bscores[scores.length]
3The last valid index of an array is its length minus 1 CThe array has 2 items, so the indexes are 0 and 1. colours[2] is undefined. Use colours[1] for the last item.
4Using length as the index reads past the end and gives undefined Dscores[scores.length - 1]
5let colours = ['red', 'blue']; console.log(colours[2]); EPrints undefined, because a 3-item array only has indexes 0, 1 and 2
6for (let i = 0; i <= scores.length; i++) { console.log(scores[i]); } FPrints undefined, because a negative index is not a valid position

Definitions

arr[arr.length]

Fill in the Blanks

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

1.
let scores = [5, 8, 6]; console.log(scores[_]);
2.
let scores = [5, 8, 6]; console.log(scores[scores.______]);
3.
let scores = [5, 8, 6]; console.log(scores[__]);
4.
let scores = [5, 8, 6]; console.log(scores[scores.length _ 1]);
5.
let scores = [5, 8, 6]; let x = scores[5]; console.log(x === _________);

Spot the Error

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

Question 1
A const scores = [6, 9, 7];
B const last = scores[3];
C console.log(last); // 8
Question 2
A const m = [70, 85, 90];
B const n = m.length;
C let sum = 0;
D for (let i = 0; i <= n; i++) {
E sum += m[i];
F }
Question 3
A const s = [4, 6, 9];
B const last = s[-1];
C console.log(last);
Question 4
A const pets = ["cat", "dog"];
B console.log(pets[2]); // dog
C console.log(pets.length); // 2
Question 5
A let i = 3;
B const items = ['a', 'b', 'c'];
C console.log(items[i]); // c

Answer Key

Reading beyond the end of an array
Section A: Quick Questions
Q1
B — undefined
Q2
A — It gives undefined and the program keeps running
Q3
A — names[names.length - 1]
Q4
A — undefined
Q5
B — undefined
Section B: Match the Pairs
Q1
let scores = [5, 8, 9]; console.log(scores[3]); → Prints undefined, because a 3-item array only has indexes 0, 1 and 2
Q2
console.log(scores[-1]); → Prints undefined, because a negative index is not a valid position
Q3
The last valid index of an array is its length minus 1 → scores[scores.length - 1]
Q4
Using length as the index reads past the end and gives undefined → scores[scores.length]
Q5
let colours = ['red', 'blue']; console.log(colours[2]); → The array has 2 items, so the indexes are 0 and 1. colours[2] is undefined. Use colours[1] for the last item.
Q6
for (let i = 0; i <= scores.length; i++) { console.log(scores[i]); } → The loop uses <= so the last pass reads scores[scores.length], which is undefined. It should use i < scores.length.
Section C: Definitions
Q1
arr[arr.length] — Reading an index past the last item (or a negative index) does not crash. It gives undefined. The last valid index is length - 1, so scores[10] in a 10-item array is undefined.
Section D: Fill in the Blanks
Q1
let scores = [5, 8, 6]; console.log(scores[3]);
Q2
let scores = [5, 8, 6]; console.log(scores[scores.length]);
Q3
let scores = [5, 8, 6]; console.log(scores[-1]);
Q4
let scores = [5, 8, 6]; console.log(scores[scores.length - 1]);
Q5
let scores = [5, 8, 6]; let x = scores[5]; console.log(x === undefined);
Section E: Spot the Error
Q1
A — The array now has only 3 items, so the last valid index is 2. scores[3] is past the end and gives undefined instead of 8. The array should have 4 items: [6, 9, 7, 8].
Q2
D — i <= n makes the loop run once too often. When i is 3, m[3] is undefined and sum becomes NaN. It should be i < n.
Q3
B — JavaScript arrays do not support negative indexes like s[-1]. It gives undefined. Use s[s.length - 1] to get the last item.
Q4
B — Indexes start at 0, so the second item is pets[1]. pets[2] is past the end of a 2-item array and gives undefined. It should be pets[1].
Q5
A — The array has 3 items, so its last index is 2. items[3] is past the end and gives undefined, not 'c'. It should be let i = 2;