Skip to content

Commit 6ff78d3

Browse files
author
Yan Kazymyr
committed
Solution
1 parent ae59795 commit 6ff78d3

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

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

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

1537
module.exports = {

0 commit comments

Comments
 (0)