Skip to content

Commit 50998f6

Browse files
committed
classes task completed
1 parent 299186d commit 50998f6

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/herbivoresAndCarnivores.js

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

33
class Animal {
44
// write your code here
5+
static alive = [];
6+
constructor(name, hidden = false) {
7+
this.name = name;
8+
this.health = 100;
9+
this.hidden = hidden;
10+
11+
Animal.alive.push(this);
12+
}
13+
14+
static getAliveList() {
15+
return Animal.alive.map((animal) => {
16+
if (animal instanceof Herbivore) {
17+
return {
18+
name: animal.name,
19+
health: animal.health,
20+
hidden: animal.hidden,
21+
};
22+
} else {
23+
return { name: animal.name, health: animal.health };
24+
}
25+
});
26+
}
527
}
628

729
class Herbivore extends Animal {
30+
constructor(name, hidden = false) {
31+
super(name, hidden);
32+
}
833
// write your code here
34+
hide() {
35+
this.hidden = true;
36+
}
937
}
1038

1139
class Carnivore extends Animal {
40+
// No custom constructor needed as it only calls the parent constructor
1241
// write your code here
42+
bite(target) {
43+
if (target instanceof Herbivore && !target.hidden) {
44+
target.health -= 50;
45+
46+
if (target.health <= 0) {
47+
Animal.alive = Animal.alive.filter((animal) => animal !== target);
48+
}
49+
}
50+
}
1351
}
1452

1553
module.exports = {

0 commit comments

Comments
 (0)