Skip to content

Commit dc5da7a

Browse files
committed
Solution
1 parent ae59795 commit dc5da7a

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
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+
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+
hidden = false;
19+
20+
hide() {
21+
this.hidden = true;
22+
}
923
}
1024

1125
class Carnivore extends Animal {
12-
// write your code here
26+
bite(target) {
27+
if (target instanceof Carnivore || target.hidden) {
28+
return;
29+
}
30+
target.health -= 50;
31+
32+
if (target.health <= 0) {
33+
target.die();
34+
}
35+
}
1336
}
1437

1538
module.exports = {

0 commit comments

Comments
 (0)