Skip to content
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
26 changes: 26 additions & 0 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,40 @@

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

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
bite(animal) {
if (animal instanceof Carnivore || animal.hidden) {
return;
}
animal.health = animal.health - 50;

if (animal.health <= 0) {
Animal.alive = Animal.alive.filter((currentAnimal) => {
return currentAnimal !== animal;
});
Comment on lines +33 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #1 violation: This code uses both splice (lines 35-36) and filter (lines 37-39) to remove the same animal, which is redundant. Use only the filter method to remove dead animals from Animal.alive. Remove the indexOf + splice logic and keep only the filter.

}
}
}

module.exports = {
Expand Down
Loading