Skip to content
Open
Changes from 1 commit
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 @@
'use strict';

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

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

die() {
const index = Animal.alive.indexOf(this);

if (index > -1) {
Animal.alive.splice(index, 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item: "The Animal.alive array must be updated using Array.prototype.filter to remove dead animals (those with health <= 0)." The current implementation uses indexOf and splice in the die() method, which is not allowed. You must use filter to update Animal.alive after an animal's health drops to 0 or below.

}
}

checkDeath() {
if (this.health <= 0) {
this.die();
}
}
}

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

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

class Carnivore extends Animal {
// write your code here
constructor(name, health = 100) {
super(name, health);
}
bite(herbivore) {
if (herbivore instanceof Herbivore && !herbivore.hidden) {
herbivore.health -= 50;
herbivore.checkDeath();
}
}
}

module.exports = {
Expand Down