OOP Studio Ages 9-15
State Challenge
Create a class with one property that changes when a method runs, then call the method twice.
Create your own state change.
Example code
class GameCharacter {
constructor(name) {
this.name = name;
this.health = 5;
}
takeHit() {
this.health -= 1;
say(this.name + " health: " + this.health);
}
}
let hero = new GameCharacter("Nova");
hero.takeHit();
hero.takeHit();
Program result
- Run your code to see output.
Type real JavaScript, then run it.
State challenge complete.
You created a method that changes object state each time it runs.
Next Lesson