← ACARA Digital Technologies Years 9–10

Building a modular program with functions, arrays and classes

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.

Understanding Building a modular program with functions, arrays and classes in ACARA Digital Technologies Years 9–10

What it does: main() does not do the work itself. It calls helpers like totalPoints() and uses the results. The class is the data part of the program. Its constructor sets the properties for every new object. The class is the blueprint for the data. Helper functions use the objects, they do not define the data. Each function should do one job. Then you can test it by itself and reuse it. The helper needs the array to search through, so the array is passed in as an argument. Small functions with one job can be checked with console.assert before you put the parts together.

Common mistakes: 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;. 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) {.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 743–781) and word search (Modules 743–809).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which design is the most modular for a program that tracks many students?
A Student class, an array of Student objects, and small helper functionsOne giant function that stores every student's data in separate variables
Matches
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
Applying
Meanings
Term
modular program
Meaning
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.
Practice
class Player {
Testing
Gaps
Student {
Check Answer
Spots
2 constructor(name, mark) {
3 name = name;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Arctic Hare' badge — Speed Indexer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
A modular program splits the work: a class holds the data, an array holds many objects, and functions each do one job.
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
The class is the blueprint for the data. Helper functions use the objects, they do not define the data.
3. How should you store 30 Student objects?
A. const student1, student2, student3 ... student30
B. const students = []; then students.push(new Student(...)) ✓
An array can hold many objects, and loops and helper functions can work on it easily.
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 ✓
Each function should do one job. Then you can test it by itself and reuse it.
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
The main function is like a manager: it creates the array and calls the helpers, but it does not do all the work itself.

Match the Pairs

const team = [new Player('Ava', 12), new Player('Leo', 9)]; → Creates an array that holds many objects made from the Player class
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
A main function runs the steps by calling small helper functions in order → function main() { const total = totalPoints(team); console.log(total); }
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; } }
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
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

Key Term

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.

Fill in the Blanks

1. class Student { constructor(name, mark) { this.name = name;
Hint: Type the keyword that starts a class definition, which holds the data for each student.
2. const students = []; students.push(new Student("Ava", 88));
Hint: Type the array method that adds a new object to the end of the array.
3. function findStudent(list, name) { return list.find(s => s.name === name); }
Hint: Type the array method that searches for and returns the first matching object.
4. function main() { console.log(totalMarks(students)); }
Hint: Type the name of the array variable that holds all the Student objects.
5. const test = [new Student("Zoe", 50)]; console.assert(totalMarks(test) === 50);
Hint: Type the console method that checks a test condition is true and reports a failure if it is not.

Spot the Error

Question 1
A class Student {
B constructor(name, mark) {
C name = name;
D this.mark = mark;
E }
F }
Line 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;
Question 2
A function total(list) {
B let sum = 0;
C for (const s in list) {
D sum += s.mark;
E }
F return sum; }
Line 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) {
Question 3
A function Book {
B constructor(title, pages) {
C this.title = title;
D this.pages = pages;
E }
F }
Line A — A class is declared with the class keyword, not function. It should be class Book {
Question 4
A const pets = [];
B pets.push(new Pet('Rex', 3));
C pets.push(Pet('Kit', 5));
D console.log(pets.length);
Line 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));
Question 5
A function main() {
B const list = makeList;
C console.log(total(list));
D }
E main();
Line 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();

Student with a class code? Join your class →