diff --git a/src/herbivoresAndCarnivores.js b/src/herbivoresAndCarnivores.js index 637b47ba4..4320ec3de 100644 --- a/src/herbivoresAndCarnivores.js +++ b/src/herbivoresAndCarnivores.js @@ -1,15 +1,47 @@ 'use strict'; class Animal { - // write your code here + constructor(name) { + this.health = 100; + this.name = name; + Animal.alive.push(this); + } + + die() { + Animal.alive = Animal.alive.filter((animal) => animal !== this); + } + + static alive = []; } class Herbivore extends Animal { - // write your code here + constructor(name) { + super(name); + this.hidden = false; + } + + hide() { + this.hidden = true; + } } class Carnivore extends Animal { - // write your code here + // eslint-disable-next-line no-useless-constructor + constructor(name) { + super(name); + } + + bite(target) { + if (target instanceof Herbivore) { + if (target.hidden === false) { + target.health -= 50; + + if (target.health === 0) { + target.die(); + } + } + } + } } module.exports = {