maymunskoa

03. Nikulden’s meals (Final Exam 071219)

Mar 13th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let guests = {};
  4. let unlikeCount = 0;
  5.  
  6. for (const line of input) {
  7.     if(line === 'Stop'){
  8.         break;
  9.     }
  10.     let [reaction, name, meal] = line.split('-');
  11.    
  12.     switch(reaction){
  13.         case 'Like':
  14.             if(!guests.hasOwnProperty(name)){
  15.                 guests[name] = [];
  16.                 guests[name].push(meal);
  17.             }else{
  18.                 if(!guests[name].includes(meal)){
  19.                     guests[name].push(meal);
  20.                 }
  21.             }
  22.             break;
  23.  
  24.         case 'Unlike':
  25.             if(!guests.hasOwnProperty(name)){
  26.                 console.log(`${name} is not at the party.`);
  27.             }else{
  28.                 let index = guests[name].indexOf(meal);
  29.                 if(index > -1){
  30.                      guests[name].splice(index,1);
  31.                      console.log(`${name} doesn't like the ${meal}.`);
  32.                     unlikeCount++;
  33.                }else{
  34.                    console.log(`${name} doesn't have the ${meal} in his/her collection.`);
  35.                 }
  36.             }
  37.  
  38.             break;
  39.     }
  40.    
  41. }
  42.  
  43.     Object.entries(guests)
  44.     .sort((a,b)=>{
  45.         if(b[1].length === a[1].length){
  46.             return a[0].localeCompare(b[0]);
  47.         }
  48.             return b[1].length - a[1].length;
  49.     }).forEach(pair=>{
  50.         let name = pair[0];
  51.         let list = pair[1]
  52.         console.log(`${name}: ${list.join(', ')}`);
  53.        
  54.     });
  55.     console.log(`Unliked meals: ${unlikeCount}`);
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment