Skip to content

Commit 01fd260

Browse files
author
Hanna
committed
add task solution1
1 parent ae59795 commit 01fd260

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)) {
28+
return;
29+
}
30+
31+
if (target.hidden === true) {
32+
return;
33+
}
34+
35+
target.health -= 50;
36+
37+
if (target.health <= 0) {
38+
Animal.alive = Animal.alive.filter((animal) => animal !== target);
39+
}
40+
}
1341
}
1442

1543
module.exports = {

0 commit comments

Comments
 (0)