From 50ea0708698a805530b3ddcb02050d752f83656f Mon Sep 17 00:00:00 2001 From: Artem Hryb Date: Sun, 12 Jan 2025 15:35:04 +0200 Subject: [PATCH] add solution --- src/herbivoresAndCarnivores.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/herbivoresAndCarnivores.js b/src/herbivoresAndCarnivores.js index 637b47ba4..effcca102 100644 --- a/src/herbivoresAndCarnivores.js +++ b/src/herbivoresAndCarnivores.js @@ -1,15 +1,39 @@ 'use strict'; class Animal { - // write your code here + static alive = []; + + constructor(name) { + this.name = name; + this.health = 100; + Animal.alive.push(this); + } + + checkHealth() { + if (this.health <= 0) { + Animal.alive = Animal.alive.filter((animal) => animal !== this); + } + } } 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 + bite(target) { + if (target instanceof Herbivore && !target.hidden) { + target.health -= 50; + target.checkHealth(); + } + } } module.exports = {