Classes and Methods Homework
Classes and Methods Homework
// CODE_RUNNER: Classes and Methods Homework
class Player {
constructor(name, health) {
this.name = name;
this.health = health;
}
takeDamage() {
this.health -= 20;
if (this.health < 0) {
this.health = 0;
}
console.log(this.name + " took damage! Health is now " + this.health);
}
heal() {
this.health += 15;
console.log(this.name + " healed! Health is now " + this.health);
}
}
// Example usage:
let hero = new Player("Knight", 100);
hero.takeDamage();
hero.takeDamage();
hero.heal();