Skip to content

Commit 5538dc3

Browse files
committed
Solution
1 parent ae59795 commit 5538dc3

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
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+
Animal.alive.push(this);
11+
}
512
}
613

714
class Herbivore extends Animal {
8-
// write your code here
15+
constructor(name, health = 100) {
16+
super(name, health);
17+
18+
this.hidden = false;
19+
}
20+
21+
hide() {
22+
this.hidden = true;
23+
}
924
}
1025

1126
class Carnivore extends Animal {
12-
// write your code here
27+
constructor(name, health = 100) {
28+
super(name, health);
29+
}
30+
31+
bite(herbivore) {
32+
if (!(herbivore instanceof Herbivore) || herbivore.hidden) {
33+
return;
34+
}
35+
36+
herbivore.health -= 50;
37+
38+
if (herbivore.health <= 0) {
39+
Animal.alive = Animal.alive.filter((animal) => animal !== herbivore);
40+
}
41+
}
1342
}
1443

1544
module.exports = {

0 commit comments

Comments
 (0)