There was an error while loading. Please reload this page.
1 parent ae59795 commit 39bf1daCopy full SHA for 39bf1da
1 file changed
src/herbivoresAndCarnivores.js
@@ -1,15 +1,43 @@
1
'use strict';
2
3
class Animal {
4
- // write your code here
+ static alive = [];
5
+
6
+ constructor(name) {
7
+ this.health = 100;
8
+ this.name = name;
9
10
+ Animal.alive.push(this);
11
+ }
12
}
13
14
class Herbivore extends Animal {
15
16
+ super(name);
17
+ this.hidden = false;
18
19
20
+ hide() {
21
+ this.hidden = true;
22
23
24
25
class Carnivore extends Animal {
26
+ bite(target) {
27
+ if (!(target instanceof Herbivore) || target.hidden === true) {
28
+ return;
29
30
31
+ target.health -= 50;
32
33
+ if (target.health <= 0) {
34
+ const indexOfDead = Animal.alive.indexOf(target);
35
36
+ if (indexOfDead !== -1) {
37
+ Animal.alive.splice(indexOfDead, 1);
38
39
40
41
42
43
module.exports = {
0 commit comments