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 #1808

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
37 changes: 34 additions & 3 deletions src/herbivoresAndCarnivores.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
'use strict';

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

Choose a reason for hiding this comment

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

Suggested change
static alive = [];


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

Choose a reason for hiding this comment

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

Suggested change
this.name = name;
this.health = health;
Animal.alive.push(this);
this.name = name;
this.health = health;
Animal.alive.push(this);

}

static removeFromAlive(animal) {
const index = Animal.alive.indexOf(animal);
if (index !== -1) {
Animal.alive.splice(index, 1);
}
}

Choose a reason for hiding this comment

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

You can use only 1 filter instead, which will clear the array from animals with health less than or equal to 0

}

Choose a reason for hiding this comment

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

#952
That's why you have failed tests

Suggested change
Animal.alive = [];

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
constructor(name) {
super(name);
}

bite(animal) {
if (!(animal instanceof Carnivore) && !animal.hidden) {

Choose a reason for hiding this comment

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

Just check if animal instanceof Herbivore instead.

Suggested change
if (!(animal instanceof Carnivore) && !animal.hidden) {
if (animal instanceof Herbivore && !animal.hidden) {

animal.health -= 50;
if (animal.health <= 0) {
Animal.removeFromAlive(animal);
Copy link

Choose a reason for hiding this comment

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

This function doesn't take any parameters

}
}
}
}

module.exports = {
Expand Down