Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wildZoo(input) {
- let commandsObject = {Add: add,Feed: feed,};
- let 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, infoLine]) => {
- console.log(`${animal} -> ${infoLine.foodNeeded}g`);
- });
- let areas = {};
- Object.values(animals).forEach((line) => {
- if (!areas[line.area]) areas[line.area] = 0;
- areas[line.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`);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement