Advertisement
bebo231312312321

Untitled

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