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
33 changes: 30 additions & 3 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
'use strict';

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

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

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(bicho) {
if (bicho instanceof Herbivore && bicho.hidden === false) {
bicho.health -= 50;

if (bicho.health <= 0) {
const index = Animal.alive.indexOf(bicho);

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.

Checklist item #1 violation: Use filter instead of indexOf + splice to remove dead animals from the array. Replace the index lookup and splice with Animal.alive = Animal.alive.filter(animal => animal !== bicho) or filter by health condition.

}
}
}
}

module.exports = {
Expand Down
Loading