From 62acae729a657c338bc27db6fb3e75a430e7db56 Mon Sep 17 00:00:00 2001 From: Maria Date: Tue, 7 Jan 2025 15:31:58 +0300 Subject: [PATCH] Solution --- src/herbivoresAndCarnivores.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/herbivoresAndCarnivores.js b/src/herbivoresAndCarnivores.js index 637b47ba4..8b1e9e2b5 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; + Animal.alive.push(this); + } + + die() { + if (this.health <= 0) { + Animal.alive = Animal.alive.filter((animal) => animal !== this); + } + } } class Herbivore extends Animal { - // write your code here + constructor(name, health, hidden) { + super(name, health); + + this.hidden = hidden || false; + } + + hide() { + this.hidden = true; + } } class Carnivore extends Animal { - // write your code here + bite(beast) { + if (beast instanceof Herbivore && !beast.hidden) { + beast.health -= 50; + beast.die(); + } + } } module.exports = {