Skip to content

Commit 194ab38

Browse files
committed
Solution
1 parent ae59795 commit 194ab38

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
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+
Animal.alive.push(this);
10+
}
11+
12+
checkAlive() {
13+
if (this.health <= 0) {
14+
const index = Animal.alive.indexOf(this);
15+
16+
if (index !== -1) {
17+
Animal.alive.splice(index, 1);
18+
}
19+
}
20+
}
521
}
622

723
class Herbivore extends Animal {
8-
// write your code here
24+
constructor(name) {
25+
super(name);
26+
this.hidden = false;
27+
}
28+
29+
hide() {
30+
this.hidden = true;
31+
}
932
}
1033

1134
class Carnivore extends Animal {
12-
// write your code here
35+
bite(target) {
36+
if (!(target instanceof Herbivore)) {
37+
return;
38+
}
39+
40+
if (target.hidden) {
41+
return;
42+
}
43+
44+
target.health -= 50;
45+
46+
if (target.health < 0) {
47+
target.health = 0;
48+
}
49+
50+
target.checkAlive();
51+
}
1352
}
1453

1554
module.exports = {

0 commit comments

Comments
 (0)