Building a modular program with functions, arrays and classes

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

Building a modular program with functions, arrays and classes

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which design is the most modular for a program that tracks many students?
A A Student class, an array of Student objects, and small helper functions
B One giant function that stores every student's data in separate variables
2. Where should the data for one student (name, mark) be defined in a modular program?
A In a class such as class Student
B Inside the function that calculates the average
3. How should you store 30 Student objects?
A const student1, student2, student3 ... student30
B const students = []; then students.push(new Student(...))
4. Which helper function follows the 'one job' rule?
A function doEverything(students) { ... } that totals, searches, prints and sorts
B function totalMarks(students) { ... } that only adds up the marks
5. What is the job of a main function in a modular program?
A To run the steps by calling the other functions in order
B To replace the class and store all the data

Match the Pairs

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

Left columnRight column
1const team = [new Player('Ava', 12), new Player('Leo', 9)]; AUses = (assignment) instead of === (comparison), so the search changes the data and finds the wrong object
2function totalPoints(players) { return players.reduce((sum, p) => sum + p.points, 0); } BThe helper never adds the points or returns a value, so it gives back undefined instead of a total
3A main function runs the steps by calling small helper functions in order CCreates an array that holds many objects made from the Player class
4A class holds the data for each object, so one blueprint makes many objects Dfunction main() { const total = totalPoints(team); console.log(total); }
5function findPlayer(players, name) { return players.find(p => p.name = name); } EA helper function that does one job: it adds up the points of every object in the array
6function totalPoints(players) { players.forEach(p => p.points); } Fclass Player { constructor(name, points) { this.name = name; this.points = points; } }

Definitions

modular program

Fill in the Blanks

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

1.
_____ Student { constructor(name, mark) { this.name = name;
2.
const students = []; students.____(new Student("Ava", 88));
3.
function findStudent(list, name) { return list.____(s => s.name === name); }
4.
function main() { console.log(totalMarks(________)); }
5.
const test = [new Student("Zoe", 50)]; console.______(totalMarks(test) === 50);

Spot the Error

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

Question 1
A class Student {
B constructor(name, mark) {
C name = name;
D this.mark = mark;
E }
F }
Question 2
A function total(list) {
B let sum = 0;
C for (const s in list) {
D sum += s.mark;
E }
F return sum; }
Question 3
A function Book {
B constructor(title, pages) {
C this.title = title;
D this.pages = pages;
E }
F }
Question 4
A const pets = [];
B pets.push(new Pet('Rex', 3));
C pets.push(Pet('Kit', 5));
D console.log(pets.length);
Question 5
A function main() {
B const list = makeList;
C console.log(total(list));
D }
E main();

Answer Key

Building a modular program with functions, arrays and classes
Section A: Quick Questions
Q1
A — A Student class, an array of Student objects, and small helper functions
Q2
A — In a class such as class Student
Q3
B — const students = []; then students.push(new Student(...))
Q4
B — function totalMarks(students) { ... } that only adds up the marks
Q5
A — To run the steps by calling the other functions in order
Section B: Match the Pairs
Q1
const team = [new Player('Ava', 12), new Player('Leo', 9)]; → Creates an array that holds many objects made from the Player class
Q2
function totalPoints(players) { return players.reduce((sum, p) => sum + p.points, 0); } → A helper function that does one job: it adds up the points of every object in the array
Q3
A main function runs the steps by calling small helper functions in order → function main() { const total = totalPoints(team); console.log(total); }
Q4
A class holds the data for each object, so one blueprint makes many objects → class Player { constructor(name, points) { this.name = name; this.points = points; } }
Q5
function findPlayer(players, name) { return players.find(p => p.name = name); } → Uses = (assignment) instead of === (comparison), so the search changes the data and finds the wrong object
Q6
function totalPoints(players) { players.forEach(p => p.points); } → The helper never adds the points or returns a value, so it gives back undefined instead of a total
Section C: Definitions
Q1
modular program — A program built from small parts: a class for the data, an array of its objects, and helper functions that each do one job (like totals or searches), all run by a main function.
Section D: Fill in the Blanks
Q1
class Student { constructor(name, mark) { this.name = name;
Q2
const students = []; students.push(new Student("Ava", 88));
Q3
function findStudent(list, name) { return list.find(s => s.name === name); }
Q4
function main() { console.log(totalMarks(students)); }
Q5
const test = [new Student("Zoe", 50)]; console.assert(totalMarks(test) === 50);
Section E: Spot the Error
Q1
C — Inside a constructor the data must be stored on the new object with this. Without it, name = name only reassigns the parameter and the object gets no name. It should be this.name = name;
Q2
C — for...in gives the index positions (as text), not the objects, so s.mark would be undefined and the total would be NaN. To visit each object use for...of: for (const s of list) {
Q3
A — A class is declared with the class keyword, not function. It should be class Book {
Q4
C — The new keyword is missing. A class must be called with new to create an object, so it should be pets.push(new Pet('Kit', 5));
Q5
B — Without brackets the function is not called, so list holds the function itself instead of the array it returns. It should be const list = makeList();