Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 1.7 KB

readme.md

File metadata and controls

55 lines (42 loc) · 1.7 KB

Herbivores and Сarnivores

Read the guideline before start

Task description:

Nature is well suited to reflect the principles of Object Oriented Programming. All instances of the Animal class must have properties health and a name. Health is 100 by default.

Create a Herbivore class. Herbivore has a method of hide, which sets the hidden property of the beast to the value of true, and helps to hide from carnivores.

Create a Сarnivore class. Carnivore has a bite method, which takes a herbivore object and decreases the object's health by 50. The method does not work if it is another сarnivore, or the herbivore is currently hiding.

Herbivore and Сarnivore should extend an Animal class.

All alive animals should be in the static Animal.alive array. If the health of the animal becomes less than or equal to 0, the beast dies and it should be removed from the static array Animals.alive.

Example:

const deer = new Herbivore('Bembi');
const panther = new Carnivore('Bagira');
const lion = new Carnivore('King');
const rabbit = new Herbivore('Max');

Animal.alive === [
  {name: 'Bembi', health: 100, hidden: false},
  {name: 'Bagira', health: 100},
  {name: 'King', health: 100},
  {name: 'Max', health: 100, hidden: false},
];

lion.bite(deer);
panther.bite(lion);

Animal.alive === [
  {name: 'Bembi', health: 50},
  {name: 'Bagira', health: 100},
  {name: 'King', health: 100},
  {name: 'Max', health: 100},
];

panther.bite(deer);
rabbit.hide();
panther.bite(rabbit);

Animal.alive === [
  {name: 'Bagira', health: 100},
  {name: 'King', health: 100},
  {name: 'Max', health: 100, hidden: true},
];