Skip to content

Commit d9d172c

Browse files
committed
Added solution
1 parent ae59795 commit d9d172c

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

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

33
class Animal {
4-
// write your code here
4+
static alive = [];
5+
6+
constructor(name, health = 100) {
7+
this.health = health;
8+
this.name = name;
9+
}
510
}
611

712
class Herbivore extends Animal {
8-
// write your code here
13+
constructor(name, health, hidden = false) {
14+
super(name, health);
15+
this.hidden = hidden;
16+
17+
Animal.alive.push(this);
18+
}
19+
20+
hide() {
21+
this.hidden = true;
22+
}
923
}
1024

1125
class Carnivore extends Animal {
12-
// write your code here
26+
constructor(name, health) {
27+
super(name, health);
28+
29+
Animal.alive.push(this);
30+
}
31+
32+
bite(prey) {
33+
if (prey instanceof Herbivore && !prey.hidden) {
34+
prey.health -= 50;
35+
36+
if (prey.health <= 0) {
37+
const index = Animal.alive.indexOf(prey);
38+
39+
if (index > -1) {
40+
Animal.alive.splice(index, 1);
41+
}
42+
}
43+
}
44+
}
1345
}
1446

1547
module.exports = {

0 commit comments

Comments
 (0)