Skip to content

Commit 1035f93

Browse files
committed
Implement Herbivores and Carnivores
1 parent ae59795 commit 1035f93

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) {
7+
this.name = name;
8+
this.health = 100;
9+
10+
Animal.alive.push(this);
11+
}
12+
13+
die() {
14+
Animal.alive = Animal.alive.filter((animal) => animal !== this);
15+
}
516
}
617

718
class Herbivore extends Animal {
8-
// write your code here
19+
constructor(name) {
20+
super(name);
21+
22+
this.hidden = false;
23+
}
24+
25+
hide() {
26+
this.hidden = true;
27+
}
928
}
1029

1130
class Carnivore extends Animal {
12-
// write your code here
31+
bite(animal) {
32+
if (!(animal instanceof Herbivore) || animal.hidden) {
33+
return;
34+
}
35+
36+
animal.health -= 50;
37+
38+
if (animal.health <= 0) {
39+
animal.die();
40+
}
41+
}
1342
}
1443

1544
module.exports = {

0 commit comments

Comments
 (0)