-
Notifications
You must be signed in to change notification settings - Fork 4.1k
add task solution #3915
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
base: master
Are you sure you want to change the base?
add task solution #3915
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,40 @@ | ||
| 'use strict'; | ||
|
|
||
| class Animal { | ||
| // write your code here | ||
| static alive = []; | ||
|
|
||
| constructor(name) { | ||
| this.name = name; | ||
| this.health = 100; | ||
| 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(animal) { | ||
| if (animal instanceof Herbivore && !animal.hidden) { | ||
| animal.health -= 50; | ||
|
|
||
| if (animal.health === 0) { | ||
| 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 commentThe 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 (with health <= 0). Avoid using nested loops or indexOf + splice for this purpose.' You are using indexOf and splice instead of filter to remove dead animals from Animal.alive. |
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 violates the requirement that animals must be removed from Animal.alive when their health is less than or equal to 0. Your code only removes them when health === 0. It should be health <= 0.