1 parent ae59795 commit dc5da7aCopy full SHA for dc5da7a
1 file changed
src/herbivoresAndCarnivores.js
@@ -1,15 +1,38 @@
1
'use strict';
2
3
class Animal {
4
- // write your code here
+ static alive = [];
5
+
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
15
}
16
17
class Herbivore extends Animal {
18
+ hidden = false;
19
20
+ hide() {
21
+ this.hidden = true;
22
23
24
25
class Carnivore extends Animal {
26
+ bite(target) {
27
+ if (target instanceof Carnivore || target.hidden) {
28
+ return;
29
30
+ target.health -= 50;
31
32
+ if (target.health <= 0) {
33
+ target.die();
34
35
36
37
38
module.exports = {
0 commit comments