Skip to content

Commit fc32bd5

Browse files
committed
Solution
1 parent ae59795 commit fc32bd5

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

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

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

715
class Herbivore extends Animal {
8-
// write your code here
16+
hidden = false;
17+
18+
hide() {
19+
this.hidden = true;
20+
}
921
}
1022

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

1535
module.exports = {

0 commit comments

Comments
 (0)