Skip to content

Commit 35a50a7

Browse files
committed
Solution
1 parent ae59795 commit 35a50a7

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

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

33
class Animal {
4-
// write your code here
4+
static alive = [];
5+
6+
constructor(name, health) {
7+
this.name = name;
8+
this.health = health || 100;
9+
Animal.alive.push(this);
10+
}
11+
12+
die() {
13+
Animal.alive = Animal.alive.filter((animal) => animal !== this);
14+
}
515
}
616

717
class Herbivore extends Animal {
8-
// write your code here
18+
constructor(name, health) {
19+
super(name, health);
20+
this.hidden = false;
21+
}
22+
23+
hide() {
24+
this.hidden = true;
25+
}
926
}
1027

1128
class Carnivore extends Animal {
12-
// write your code here
29+
bite(target) {
30+
if (target instanceof Carnivore) {
31+
return;
32+
}
33+
34+
if (target.hidden) {
35+
return;
36+
}
37+
38+
target.health -= 50;
39+
40+
if (target.health <= 0) {
41+
target.die();
42+
}
43+
}
1344
}
1445

1546
module.exports = {

0 commit comments

Comments
 (0)