Skip to content

Commit 44f3b12

Browse files
Solution 1
1 parent ae59795 commit 44f3b12

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"license": "GPL-3.0",
1818
"devDependencies": {
1919
"@mate-academy/eslint-config": "latest",
20-
"@mate-academy/scripts": "^1.8.6",
20+
"@mate-academy/scripts": "^2.1.1",
2121
"eslint": "^8.57.0",
2222
"eslint-plugin-jest": "^28.6.0",
2323
"eslint-plugin-node": "^11.1.0",

src/herbivoresAndCarnivores.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,45 @@
22

33
class Animal {
44
// write your code here
5+
static alive = [];
6+
constructor(name, health = 100) {
7+
this.name = name;
8+
this.health = health;
9+
Animal.alive.push(this);
10+
}
11+
12+
die() {
13+
Animal.alive = Animal.alive.filter((animal) => animal !== this);
14+
}
515
}
616

717
class Herbivore extends Animal {
818
// write your code here
19+
constructor(name) {
20+
super(name);
21+
this.hidden = false;
22+
}
23+
24+
hide() {
25+
this.hidden = true;
26+
}
927
}
1028

1129
class Carnivore extends Animal {
1230
// write your code here
31+
bite(target) {
32+
if (target instanceof Carnivore) {
33+
return null;
34+
}
35+
36+
if (target instanceof Herbivore && target.hidden === false) {
37+
target.health -= 50;
38+
}
39+
40+
if (target.health <= 0) {
41+
target.die();
42+
}
43+
}
1344
}
1445

1546
module.exports = {

0 commit comments

Comments
 (0)