diff --git a/src/herbivoresAndCarnivores.js b/src/herbivoresAndCarnivores.js index 637b47ba4..65ac6e426 100644 --- a/src/herbivoresAndCarnivores.js +++ b/src/herbivoresAndCarnivores.js @@ -1,15 +1,40 @@ 'use strict'; - class Animal { - // write your code here + static alive = []; + + constructor(name, health = 100) { + this.name = name; + this.health = health; + this.alive = true; + Animal.alive.push(this); + } + + isAlive() { + if (this.health <= 0) { + this.alive = false; + Animal.alive = Animal.alive.filter((element) => element.alive); + } + } } class Herbivore extends Animal { - // write your code here + constructor(name, health = 100, hidden = false) { + super(name, health); + this.hidden = hidden; + } + + hide() { + this.hidden = true; + } } class Carnivore extends Animal { - // write your code here + bite(target) { + if (target instanceof Herbivore && !target.hidden) { + target.health -= 50; + target.isAlive(); + } + } } module.exports = {