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

Solution #3346

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
41 changes: 38 additions & 3 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
/* eslint-disable prettier/prettier */
'use strict';

class Animal {
// write your code here
static alive = [];

constructor(name, health = 100) {
this.name = name;
this.health = health;
Animal.alive.push(this);
}

static removeDeadAnimals() {
Animal.alive = Animal.alive.filter((animal) => animal.health > 0);
}
}

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

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

class Carnivore extends Animal {
// write your code here
constructor(name, health = 100) {
super(name, health);
}

bite(target) {
if (
target instanceof Carnivore ||
(target instanceof Herbivore && target.hidden)
Comment on lines +35 to +37

Choose a reason for hiding this comment

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

The condition inside the if statement should only check if the target is an instance of Herbivore and is not hidden. The check for target instanceof Carnivore is unnecessary and should be removed according to the task requirements.

) {
return `${this.name} cannot bite ${target.name}.`;
}

target.health -= 50;

if (target.health <= 0) {
Animal.removeDeadAnimals();
}
}
}

module.exports = {
Expand Down
Loading