Advertisement
Alexzandur

Nikulden's meals

Dec 11th, 2019
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let col = {};
  3.     let unliked = 0;
  4.     for (let command of input) {
  5.         let cmd = command.split('-');
  6.         let guest = cmd[1];
  7.         let meal = cmd[2];
  8.         switch (cmd[0]) {
  9.             case 'Like':
  10.                 if (!col.hasOwnProperty(guest)) {
  11.                     col[guest] = [meal];
  12.                 } else {
  13.                     if (!col[guest].includes(meal)) {
  14.                         col[guest].push(meal);
  15.                     }
  16.                 }
  17.                 break;
  18.             case 'Unlike':
  19.                 if (col.hasOwnProperty(guest)) {
  20.                     if (col[guest].includes(meal)) {
  21.                         unliked++;
  22.                         col[guest].splice(col[guest].indexOf(meal), 1);
  23.                         console.log(`${guest} doesn't like the ${meal}.`);
  24.                    } else if (!col[guest].includes(meal)) {
  25.                        console.log(`${guest} doesn't have the ${meal} in his/her collection.`);
  26.                     }
  27.                 } else {
  28.                     console.log(`${guest} is not at the party.`);
  29.                 }
  30.                 break;
  31.         }
  32.     }
  33.     let arr = Object.entries(col);
  34.     arr.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]));
  35.     arr.forEach(el => {
  36.         console.log(`${el[0]}: ${el[1].join(', ').trim()}`);
  37.     });
  38.     console.log(`Unliked meals: ${unliked}`);
  39. }
  40. solve(['Like-Krisi-shrimps',
  41.     'Unlike-Vili-carp',
  42.     'Unlike-Krisi-salad',
  43.     'Unlike-Krisi-shrimps',
  44.     'Stop'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement