Skip to content

Commit 39bf1da

Browse files
committed
task solution
1 parent ae59795 commit 39bf1da

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

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

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

714
class Herbivore extends Animal {
8-
// write your code here
15+
constructor(name) {
16+
super(name);
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(target) {
27+
if (!(target instanceof Herbivore) || target.hidden === true) {
28+
return;
29+
}
30+
31+
target.health -= 50;
32+
33+
if (target.health <= 0) {
34+
const indexOfDead = Animal.alive.indexOf(target);
35+
36+
if (indexOfDead !== -1) {
37+
Animal.alive.splice(indexOfDead, 1);
38+
}
39+
}
40+
}
1341
}
1442

1543
module.exports = {

0 commit comments

Comments
 (0)