Advertisement
bebo231312312321

Untitled

Apr 3rd, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wildZoo(input) {
  2.     let commandsObject = {Add: add,Feed: feed,};
  3.  
  4.     let animals = {};
  5.     let comandLine = input.splice(0, input.indexOf("EndDay")).map(line => {
  6.         let [action,animalNames, neededFood, arena] = line.split(/[:-]/)
  7.         .map(x => isNaN(x) ? x : Number(x))
  8.      
  9.         let com = commandsObject[action]
  10.         com (animalNames, neededFood, arena)
  11.     })
  12.  
  13.     console.log(`Animals:`);
  14.  
  15.     Object.entries(animals).forEach(([animal, infoLine]) => {
  16.       console.log(`${animal} -> ${infoLine.foodNeeded}g`);
  17.     });
  18.  
  19.     let areas = {};
  20.  
  21.     Object.values(animals).forEach((line) => {
  22.       if (!areas[line.area])  areas[line.area] = 0;
  23.        
  24.       areas[line.area]++;
  25.     });
  26.     console.log('Areas with hungry animals:');
  27.  
  28.     Object.keys(areas).forEach((area) => {
  29.       console.log(` ${area}: ${areas[area]}`);
  30.     });
  31.  
  32.     function add(name, foodNeeded, area) {
  33.       if (!animals[name]) {
  34.         return (animals[name] = { foodNeeded, area });
  35.       }
  36.  
  37.       animals[name].foodNeeded += foodNeeded;
  38.       animals[name].area = area;
  39.     }
  40.  
  41.     function feed(name, food) {
  42.       if (animals[name]) {
  43.         animals[name].foodNeeded -= food;
  44.  
  45.         if (animals[name].foodNeeded <= 0) {
  46.           delete animals[name];
  47.           console.log(`${name} was successfully fed`);
  48.         }
  49.       }
  50.     }
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement