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
38 changes: 38 additions & 0 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,52 @@

class Animal {
// write your code here
static alive = [];
constructor(name, hidden = false) {
this.name = name;
this.health = 100;
this.hidden = hidden;

Animal.alive.push(this);
}

static getAliveList() {
return Animal.alive.map((animal) => {
if (animal instanceof Herbivore) {
return {
name: animal.name,
health: animal.health,
hidden: animal.hidden,
};
} else {
return { name: animal.name, health: animal.health };
}
});
}
}

class Herbivore extends Animal {
constructor(name, hidden = false) {
super(name, hidden);
}
// write your code here
hide() {
this.hidden = true;
}
}

class Carnivore extends Animal {
// No custom constructor needed as it only calls the parent constructor
// write your code here
bite(target) {
if (target instanceof Herbivore && !target.hidden) {
target.health -= 50;

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

module.exports = {
Expand Down