Skip to content

Commit 0fcdcfc

Browse files
committed
Task completed
1 parent ae59795 commit 0fcdcfc

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

src/herbivoresAndCarnivores.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
'use strict';
22

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

713
class Herbivore extends Animal {
8-
// write your code here
14+
constructor(name) {
15+
super(name);
16+
this.hidden = false;
17+
}
18+
19+
hide() {
20+
this.hidden = true;
21+
}
922
}
1023

1124
class Carnivore extends Animal {
12-
// write your code here
25+
bite(animal) {
26+
if (animal.hidden === false) {
27+
animal.health -= 50;
28+
29+
if (animal.health <= 0) {
30+
Animal.alive = Animal.alive.filter(
31+
(animalToCheck) => animalToCheck !== animal,
32+
);
33+
}
34+
}
35+
}
1336
}
1437

38+
const tiger = new Carnivore('Tiger');
39+
const zebra = new Herbivore('Zebra');
40+
41+
tiger.bite(zebra);
42+
tiger.bite(zebra);
43+
44+
console.log(Animal.alive);
45+
46+
// console.log(Animal.alive());
47+
1548
module.exports = {
1649
Animal,
1750
Herbivore,

0 commit comments

Comments
 (0)