-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
base: master
Are you sure you want to change the base?
Solution #1808
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job.
A few moments
src/herbivoresAndCarnivores.js
Outdated
@@ -1,15 +1,46 @@ | |||
'use strict'; | |||
|
|||
class Animal { | |||
// write your code here | |||
static alive = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static alive = []; |
if (index !== -1) { | ||
Animal.alive.splice(index, 1); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
Animal.alive = []; |
src/herbivoresAndCarnivores.js
Outdated
static removeFromAlive(animal) { | ||
const index = Animal.alive.indexOf(animal); | ||
if (index !== -1) { | ||
Animal.alive.splice(index, 1); | ||
} | ||
} |
There was a problem hiding this comment.
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
src/herbivoresAndCarnivores.js
Outdated
} | ||
|
||
bite(animal) { | ||
if (!(animal instanceof Carnivore) && !animal.hidden) { |
There was a problem hiding this comment.
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.
if (!(animal instanceof Carnivore) && !animal.hidden) { | |
if (animal instanceof Herbivore && !animal.hidden) { |
src/herbivoresAndCarnivores.js
Outdated
static removeFromAlive(animal) { | ||
Animal.alive = Animal.alive.filter((a) => a.health > 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so where do you use animal
here?
src/herbivoresAndCarnivores.js
Outdated
this.name = name; | ||
this.health = health; | ||
Animal.alive.push(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.name = name; | |
this.health = health; | |
Animal.alive.push(this); | |
this.name = name; | |
this.health = health; | |
Animal.alive.push(this); |
src/herbivoresAndCarnivores.js
Outdated
Animal.alive.push(this); | ||
} | ||
|
||
static removeFromAlive() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static removeFromAlive() { | |
static filterDeadAnimals() { |
src/herbivoresAndCarnivores.js
Outdated
animal.health -= 50; | ||
|
||
if (animal.health <= 0) { | ||
Animal.removeFromAlive(animal); |
There was a problem hiding this comment.
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
if (animal instanceof Herbivore && !animal.hidden) { | ||
animal.health -= 50; | ||
|
||
if (animal.health <= 0) { | ||
Animal.filterDeadAnimals(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should avoid nested if
statements - as it makes your code more difficult to read
No description provided.