OOP Studio Ages 9-15
Collection Challenge
Create an array with at least three objects, then use a loop to call one method on every object.
Create your own object team.
Example code
class PowerUp {
constructor(name, points) {
this.name = name;
this.points = points;
}
activate() {
say(this.name + " gives " + this.points + " points");
}
}
let powerUps = [
new PowerUp("Star", 10),
new PowerUp("Shield", 5),
new PowerUp("Key", 3)
];
for (let powerUp of powerUps) {
powerUp.activate();
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
Collection challenge complete.
You created a collection of objects and used a loop to call each object's method.
Next Lesson