Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arg) {
- let lineInput = arg.shift();
- const guests = [];
- while (lineInput !== 'Stop') {
- const [command, guest, meal] = lineInput.split('-');
- switch (command) {
- case 'Like':
- const guestObj = {};
- const isFound = guests.some((g) => g.name === guest);
- if (!isFound) {
- guestObj.name = guest;
- guestObj.likeMeals = [];
- guestObj.unlikeMeal = [];
- guests.push(guestObj);
- }
- guests.forEach((el) => {
- const { name, likeMeals } = el;
- const haveMeal = likeMeals.some((m) => m === meal);
- if (name === guest && !haveMeal) {
- likeMeals.push(meal);
- }
- });
- break;
- case 'Unlike':
- const isF = guests.some((g) => g.name === guest);
- if (isF) {
- guests.forEach((el) => {
- const { name, likeMeals, unlikeMeal } = el;
- if (name === guest) {
- const haveMeal = likeMeals.some((m) => m === meal);
- if (haveMeal) {
- const takeIndex = likeMeals.indexOf(meal);
- unlikeMeal.push(meal);
- likeMeals.splice(takeIndex, 1);
- console.log(`${guest} doesn't like the ${meal}.`);
- } else {
- console.log(`${guest} doesn't have the ${meal} in his/her collection.`);
- }
- }
- });
- } else {
- console.log(`${guest} is not at the party.`);
- }
- break;
- default:
- break;
- }
- lineInput = arg.shift();
- }
- const sortGuest = guests.sort((a, b) => b.likeMeals.length - a.likeMeals.length || a.name.localeCompare(b.name));
- let totalUnlikeMeal = 0;
- sortGuest.forEach((el) => {
- const { name, likeMeals, unlikeMeal } = el;
- totalUnlikeMeal += unlikeMeal.length;
- console.log(`${name}: ${likeMeals.join(', ')}`);
- });
- console.log(`Unliked meals: ${totalUnlikeMeal}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement