Skip to content

Commit 79188f9

Browse files
Solution
1 parent ae59795 commit 79188f9

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
'use strict';
22

33
class Animal {
4-
// write your code here
4+
static alive = [];
5+
6+
constructor(name, health = 100) {
7+
this.name = name;
8+
this.health = health;
9+
10+
if (health > 0) {
11+
Animal.alive.push(this);
12+
}
13+
}
514
}
615

716
class Herbivore extends Animal {
8-
// write your code here
17+
hidden = false;
18+
19+
hide() {
20+
this.hidden = true;
21+
}
922
}
1023

1124
class Carnivore extends Animal {
12-
// write your code here
25+
bite(animal) {
26+
if (animal instanceof Herbivore && !animal.hidden && animal.health > 0) {
27+
animal.health -= 50;
28+
29+
if (animal.health <= 0) {
30+
Animal.alive = Animal.alive.filter((item) => item.health > 0);
31+
}
32+
}
33+
}
1334
}
1435

1536
module.exports = {

0 commit comments

Comments
 (0)