OOP Studio Ages 9-15
Collection Builder
Students use arrays of objects to update a small team of characters.
Update a team.
Example code
class TeamMember {
constructor(name, role) {
this.name = name;
this.role = role;
this.points = 0;
}
earn(points) {
this.points += points;
}
report() {
say(this.name + " the " + this.role + ": " + this.points + " points");
}
}
let team = [
new TeamMember("Nova", "builder"),
new TeamMember("Milo", "debugger"),
new TeamMember("Kai", "designer")
];
for (let member of team) {
member.earn(5);
member.report();
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
Collection builder complete.
You updated several objects with the same method inside one loop.
Try Collection Challenge