Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,43 @@

class Animal {
// write your code here
static alive = [];

constructor(name) {
this.name = name;
this._health = 100;
Animal.alive.push(this);
}

get health() {
return this._health;
}

set health(value) {
this._health = value;

if (this._health <= 0) {
Animal.alive = Animal.alive.filter((animal) => animal.health > 0);
}
}
}

class Herbivore extends Animal {
// write your code here
hidden = false;

hide() {
this.hidden = true;
}
}

class Carnivore extends Animal {
// write your code here
bite(herbivore) {
if (herbivore instanceof Herbivore && !herbivore.hidden) {
herbivore.health -= 50;
}
}
}

module.exports = {
Expand Down
Loading