Advertisement
bebo231312312321

Untitled

Apr 2nd, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wildZoo(arr) {
  2.   const commands = {
  3.     Add: add,
  4.     Feed: feed,
  5.   };
  6.  
  7.   const animals = {};
  8.  
  9.   let n = 0;
  10.  
  11.   while (arr[n] !== 'EndDay') {
  12.     let [action, animalName, t1, t2] = arr[n].split(/[:-]/);
  13.     t1 = Number(t1);
  14.  
  15.     const fn = commands[action];
  16.     fn(animalName, t1, t2);
  17.     n++;
  18.   }
  19.  
  20.   console.log(`Animals:`);
  21.  
  22.   Object.entries(animals).forEach(([animal, animalInfo]) => {
  23.     console.log(`${animal} -> ${animalInfo.foodNeeded}g`);
  24.   });
  25.  
  26.   const areas = {};
  27.  
  28.   Object.values(animals).forEach((info) => {
  29.     if (!areas[info.area]) {
  30.       areas[info.area] = 0;
  31.     }
  32.  
  33.     areas[info.area]++;
  34.   });
  35.  
  36.   console.log('Areas with hungry animals:');
  37.  
  38.   Object.keys(areas).forEach((area) => {
  39.     console.log(` ${area}: ${areas[area]}`);
  40.   });
  41.  
  42.   function add(name, foodNeeded, area) {
  43.     if (!animals[name]) {
  44.       return (animals[name] = { foodNeeded, area });
  45.     }
  46.  
  47.     animals[name].foodNeeded += foodNeeded;
  48.     animals[name].area = area;
  49.   }
  50.  
  51.   function feed(name, food) {
  52.     if (animals[name]) {
  53.       animals[name].foodNeeded -= food;
  54.  
  55.       if (animals[name].foodNeeded <= 0) {
  56.         delete animals[name];
  57.         console.log(`${name} was successfully fed`);
  58.       }
  59.     }
  60.   }
  61. }
  62.  
  63. wildZoo([
  64.   'Add: Adam-4500-ByTheCreek',
  65.   'Add: Maya-7600-WaterfallArea',
  66.   'Add: Maya-1230-WaterfallArea',
  67.   'Feed: Jamie-2000',
  68.   'EndDay',
  69. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement