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
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 = [];

health = 100;

constructor(name) {
this.name = name;

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

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

if (index !== -1) {
Animal.alive.splice(index, 1);
Comment on lines +31 to +35

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 #1: 'Avoid nesting loops (indexOf + splice), use filter instead'. The splice method modifies the array in place while filter creates a new array - filter is the preferred approach for removing dead animals from Animal.alive.

}
}
}
}
}

module.exports = {
Expand Down
Loading