Storing objects in an array

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

Storing objects in an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates a new Student object and adds it to the end of the students array?
A students.push(Student('Ava', 7));
B students.push(new Student('Ava', 7));
2. After these lines run, how many objects are in the array? let students = []; students.push(new Student('Ava', 7)); students.push(new Student('Liam', 8));
A 2
B 0
3. Which loop visits every Student object in the students array and calls its method?
A for (const s of students) { s.introduce(); }
B for (const s of Student) { s.introduce(); }
4. Inside a for...of loop with 'const s of students', how do you read one student's points?
A s.points
B students.points
5. You want the total of every student's points. Which is correct?
A let total = 0; for (const s of students) { total = s.points; }
B let total = 0; for (const s of students) { total += s.points; }

Match the Pairs

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

Left columnRight column
1students.push(new Student('Ava', 7)); Afor (const s of students) { if (s.name === 'Ava') { found = s; } }
2for (const s of students) { s.greet(); } BCreates a new Student object and adds it to the end of the students array
3To find the total of one property across all objects in an array, add it up inside a loop CVisits each Student object in the array and calls its greet() method
4To find an object by a property, loop through the array and check each object's property DIt reads points from the whole array instead of the current object. It should be total += s.points;
5students.push(Student('Ava', 7)); EThe new keyword is missing, so Student is called like a normal function and no object is created. It should be students.push(new Student('Ava', 7));
6for (const s of students) { total += students.points; } Flet total = 0; for (const s of students) { total += s.points; }

Definitions

students.push(new Student('Ava', 7));

Fill in the Blanks

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

1.
students.____(new Student('Ava', 7));
2.
for (const s __ students) { s.greet(); }
3.
for (const s of students) { if (s.name ___ 'Ava') found = s; }
4.
let total = 0; for (const s of students) total __ s.points;
5.
const students = []; students.push(___ Student('Liam', 8));

Spot the Error

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

Question 1
A const list = [];
B list.push(new Student("Ava"));
C list.push(Student("Zoe"));
D console.log(list.length);
Question 2
A let total = 0;
B for (const s in list) {
C total += s.points;
D }
E console.log(total);
Question 3
A const list = {};
B list.push(new Student("Ava"));
C list.push(new Student("Zoe"));
D console.log(list.length);
Question 4
A let found = null;
B for (const s of list) {
C if (s.name === "Zoe") {
D found == s;
E }
F }
Question 5
A const list = [];
B list.add(new Student("Ava"));
C for (const s of list) {
D s.sayHi();
E }

Answer Key

Storing objects in an array
Section A: Quick Questions
Q1
B — students.push(new Student('Ava', 7));
Q2
A — 2
Q3
A — for (const s of students) { s.introduce(); }
Q4
A — s.points
Q5
B — let total = 0; for (const s of students) { total += s.points; }
Section B: Match the Pairs
Q1
students.push(new Student('Ava', 7)); → Creates a new Student object and adds it to the end of the students array
Q2
for (const s of students) { s.greet(); } → Visits each Student object in the array and calls its greet() method
Q3
To find the total of one property across all objects in an array, add it up inside a loop → let total = 0; for (const s of students) { total += s.points; }
Q4
To find an object by a property, loop through the array and check each object's property → for (const s of students) { if (s.name === 'Ava') { found = s; } }
Q5
students.push(Student('Ava', 7)); → The new keyword is missing, so Student is called like a normal function and no object is created. It should be students.push(new Student('Ava', 7));
Q6
for (const s of students) { total += students.points; } → It reads points from the whole array instead of the current object. It should be total += s.points;
Section C: Definitions
Q1
students.push(new Student('Ava', 7)); — Makes a new Student object and adds it to the end of the students array. You can then loop over the array and call each object's methods.
Section D: Fill in the Blanks
Q1
students.push(new Student('Ava', 7));
Q2
for (const s of students) { s.greet(); }
Q3
for (const s of students) { if (s.name === 'Ava') found = s; }
Q4
let total = 0; for (const s of students) total += s.points;
Q5
const students = []; students.push(new Student('Liam', 8));
Section E: Spot the Error
Q1
C — Student is a class, so you must use the new keyword to make an object before pushing it. It should be list.push(new Student("Zoe"));
Q2
B — for...in gives the index positions ("0", "1"...), not the Student objects, so s.points is undefined and the total becomes NaN. Use for...of to visit each object.
Q3
A — {} makes an object, which has no push method, so list.push causes a TypeError. An array of Students must be created with [] instead.
Q4
D — == compares values instead of storing one. found would stay null. Use a single = to assign the matching Student: found = s;
Q5
B — Arrays do not have an add method. To put a new object on the end of an array, use push: list.push(new Student("Ava"));