Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #3329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
'use strict';

class Animal {
// write your code here
constructor(name) {
this.health = 100;
this.name = name;
Animal.alive.push(this);
}

die() {
Animal.alive = Animal.alive.filter((animal) => animal !== this);
}

static alive = [];
}

class Herbivore extends Animal {
// write your code here
constructor(name) {
super(name);
this.hidden = false;
}

hide() {
this.hidden = true;
}
}

class Carnivore extends Animal {
// write your code here
// eslint-disable-next-line no-useless-constructor
constructor(name) {
super(name);
}

bite(target) {
if (target instanceof Herbivore) {
if (target.hidden === false) {
target.health -= 50;

if (target.health === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition if (target.health === 0) should be changed to if (target.health <= 0) to ensure that the die method is called when the target's health is less than or equal to 0, not just when it is exactly 0.

target.die();
}
}
}
}
}

module.exports = {
Expand Down
Loading