Професионална програма
+ Нов въпрос
dstoianov891 avatar dstoianov891 18 Точки

Feed the animals JavaScript final exam preparation


Имам малък проблем. Реших задачата, но един тест не минава и не мога да си открия грешката. Задачата е 90/100 и не мога да разбера защо :) Много ще съм благодарен, ако някой погледне кода и види защо последния тест не минава. Много благодаря!



Feed the Animals

The sanctuary needs to provide food for the animals and feed them, so your task is to help with the process

Create a program that organizes the daily feeding of animals. You need to keep information about animals, their daily food limit and the areas of the Wildlife Refuge they live in. You will be receiving lines with commands until you receive the "Last Info" message.  There are two possible commands:

  • "Add:{animalName}:{dailyFoodLimit}:{area}":
    • Add the animal and its daily food limit to your records. It is guaranteed that the names of the animals are unique and there will never be animals with the same name. If it already exists, just increase the value of the daily food limit with the current one that is given.
  • "Feed:{animalName}:{food}:{area}":
    • Check if the animal exists and if it does, reduce its daily food limit with the given food for feeding. If its limit reaches 0 or less, the animal is considered successfully fed and you need to remove it from your records and print the following message:
      • "{animalName} was successfully fed"

You need to know the count of hungry animals there are left in each area in the end. If an animal has daily food limit above 0, it is considered hungry.

In the end, you have to print each animal with its daily food limit sorted in descending order by the daily food limit and then by its name in ascending order in the following format:


{animalName} -> {dailyFoodLimit}g

{animalName} -> {dailyFoodLimit}g

Afterwards, print the areas with the count of animals, which are not fed in descending order by the count of animals. If an area has 0 hungry animals in it, don't print it. The output must be in the following format:

Areas with hungry animals:

{areaName} : {countOfUnfedAnimals}

{areaName} : {countOfUnfedAnimals}

Input / Constraints

  • You will be receiving lines until you receive the "Last Info" command.
  • The food comes in grams and is an integer number in the range [1...100000].
  • The input will always be valid.
  • There will never be a case, in which an animal is in two or more areas at the same time.


  • Print the appropriate message after the "Feed" command, if an animal is fed.
  • Print the animals with their daily food limit in the format described above.
  • Print the areas with the count of unfed animals in them in the format described above.














Last Info

Adam was successfully fed

Jamie was successfully fed


Maya -> 8830g

Gem -> 8730g

Bobbie -> 270g

Areas with hungry animals:

WaterfallArea : 2

DeepWoodsArea : 1


First, we receive the "Add" command, so we add "Maya" to our records and we keep her daily food limit - 7600. We know that she is in WaterfallArea. We keep adding the new animals until we receive "Maya" again and we have to increase her food limit with 1230, so it becomes 8830. After that we receive "Jamie" and we need to increase his daily food limit with 560, after which it becomes 1850. Then we start receiving "Feed" commands. First, we must decrease Bobbie's food limit with 6300, so it becomes 270. Then, we need to decrease Adam's food limit with 4650. It becomes less than zero and we remove him from the collection – he is considered fed, respectively that is one less hungry animal in the area that he is in – ByTheCreek. Then we "Feed" Jamie with 2000 and his limit becomes less than zero, so we print "Jamie was successfully fed" and we remove him from our records and note that there is one less hungry animal in his area – RiverArea. In the end, we print the animals we still have in our collection, with their daily food limits in descending order by the food limits. Afterwards we print only the areas in which there are remaining hungry animals and their count in descending order.










Last Info

Sam was succesfully fed


Maya -> 2170g

Bonie -> 190g

Areas with hungry animals:

RiverArea : 1

ByTheCreek : 1





JavaScript Fundamentals 29/07/2019 17:01:16
willystyle avatar willystyle 2466 Точки

Според мен, проблема идва от втория изход по зони,

там правиш ненужно второ сортиране по име, което го няма по условие, това е явно за да ти паснат повече случаи,

но всъщност трябва да са запазени по реда на вкарване и после сортирани по брой животни.

За да се избегне малко съм ти трансформирал структурите от данни.


NqkoiSiSmile avatar NqkoiSiSmile 5 Точки