Skip to content

Commit ba4bbfd

Browse files
committed
add Solution
1 parent ae59795 commit ba4bbfd

4 files changed

Lines changed: 72 additions & 8 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches: [ master ]
6+
7+
jobs:
8+
build:
9+
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
node-version: [20.x]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
- run: npm install
23+
- run: npm test

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.3",
2121
"eslint": "^8.57.0",
2222
"eslint-plugin-jest": "^28.6.0",
2323
"eslint-plugin-node": "^11.1.0",

src/herbivoresAndCarnivores.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1+
/* eslint-disable prettier/prettier */
12
'use strict';
23

34
class Animal {
4-
// write your code here
5+
static alive = [];
6+
7+
constructor(name, health = 100) {
8+
this.name = name;
9+
this.health = health;
10+
Animal.alive.push(this);
11+
}
12+
13+
get health() {
14+
return this._health;
15+
}
16+
17+
set health(value) {
18+
this._health = value;
19+
20+
if (this._health <= 0) {
21+
this.die();
22+
}
23+
}
24+
25+
die() {
26+
Animal.alive = Animal.alive.filter((animal) => animal !== this);
27+
}
528
}
629

730
class Herbivore extends Animal {
8-
// write your code here
31+
constructor(name, health = 100) {
32+
super(name, health);
33+
this.hidden = false;
34+
}
35+
36+
hide() {
37+
this.hidden = true;
38+
}
939
}
1040

1141
class Carnivore extends Animal {
12-
// write your code here
42+
bite(herbivore) {
43+
if (!(herbivore instanceof Herbivore)) {
44+
return;
45+
}
46+
47+
if (herbivore.hidden) {
48+
return;
49+
}
50+
51+
herbivore.health -= 50;
52+
}
1353
}
1454

1555
module.exports = {

0 commit comments

Comments
 (0)