Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wildZoo(input) {
- const commandsObject = {Add: add,Feed: feed,};
- const animals = {};
- let comandLine = input.splice(0, input.indexOf("EndDay")).map(line => {
- let [action,animalNames, neededFood, arena] = line.split(/[:-]/)
- .map(x => isNaN(x) ? x : Number(x))
- let com = commandsObject[action]
- com (animalNames, neededFood, arena)
- })
- console.log(`Animals:`);
- Object.entries(animals).forEach(([animal, animalInfo]) => {
- console.log(`${animal} -> ${animalInfo.foodNeeded}g`);
- });
- const areas = {};
- Object.values(animals).forEach((info) => {
- if (!areas[info.area]) {
- areas[info.area] = 0;
- }
- areas[info.area]++;
- });
- console.log('Areas with hungry animals:');
- Object.keys(areas).forEach((area) => {
- console.log(` ${area}: ${areas[area]}`);
- });
- function add(name, foodNeeded, area) {
- if (!animals[name]) {
- return (animals[name] = { foodNeeded, area });
- }
- animals[name].foodNeeded += foodNeeded;
- animals[name].area = area;
- }
- function feed(name, food) {
- if (animals[name]) {
- animals[name].foodNeeded -= food;
- if (animals[name].foodNeeded <= 0) {
- delete animals[name];
- console.log(`${name} was successfully fed`);
- }
- }
- }
- }
- wildZoo([
- "Add: Jamie-600-WaterfallArea",
- "Add: Maya-6570-WaterfallArea",
- "Add: Adam-4500-ByTheCreek",
- "Add: Bobbie-6570-WaterfallArea",
- "Feed: Jamie-2000",
- "Feed: Adam-2000",
- "Feed: Adam-2500",
- "EndDay"
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement