-Enigmos-

degustationParty.js

Apr 10th, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function degustationParty(input) {
  2.     let index = 0;
  3.     let line = input[index];
  4.     index++;
  5.     let record = {};
  6.     let dislikedCounter = 0;
  7.  
  8.     while (line !== "Stop") {
  9.         let [command, username, meal] = line.split('-');
  10.         let collection = [];
  11.  
  12.         if (command === 'Like') {
  13.             if (!record.hasOwnProperty(username)) {
  14.                 record[username] = collection;
  15.                 record[username].push(meal);
  16.             } else if (!collection.includes(meal)) {
  17.                 record[username].push(meal);
  18.             }
  19.         } else if (command === 'Dislike') {
  20.             if (!record.hasOwnProperty(username)) {
  21.                 console.log(username, 'is not at the party.');
  22.             } else if (!record[username].includes(meal)) {
  23.                 console.log(`${username} doesn't have the ${meal} in his/her collection.`);
  24.            } else {
  25.                dislikedCounter++;
  26.                let mealIndex = record[username].indexOf(meal);
  27.                record[username].splice(mealIndex, 1);
  28.                console.log(`${username} doesn't like the ${meal}`);
  29.             }
  30.         }
  31.  
  32.         line = input[index];
  33.         index++;
  34.     }
  35.     for (username in record) {
  36.         console.log(`${username}: ${record[username].join(', ')}`);
  37.     }
  38.     console.log(`Unliked meals: ${dislikedCounter}`);
  39. }
  40.  
  41. degustationParty(["Like-Krisi-shrimps",
  42.     "Like-Krisi-soup",
  43.     "Like-Penelope-dessert",
  44.     "Like-Misho-salad",
  45.     "Stop"]);
  46.  
  47. console.log('---');
  48.  
  49. degustationParty(["Like-Krisi-shrimps",
  50.     "Dislike-Vili-carp",
  51.     "Dislike-Krisi-salad",
  52.     "Stop"]);
  53.  
  54. console.log('---');
  55.  
  56. degustationParty(["Like-Katy-fish",
  57.     "Dislike-Katy-fish",
  58.     "Stop"]);
Advertisement
Add Comment
Please, Sign In to add comment