Ivankooo1

nikulden-best sorting

Mar 21st, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function nikulden(param){
  2.   let records = {};
  3.   let unlikedMeals = 0;
  4.   for(let el of param){
  5.  
  6.     if(el === 'Stop'){
  7.       break;
  8.     }
  9.  
  10.     let [command,name,meal] = el.split('-');
  11.     switch(command){
  12.       case 'Like':
  13.  
  14.         if(!records[name]){
  15.           records[name] = [];
  16.         }
  17.    
  18.         if(!records[name].includes(meal)){
  19.           records[name].push(meal);
  20.         }
  21.          
  22.        
  23.       ;break;
  24.       case 'Unlike':
  25.  
  26.         if(records[name]){
  27.  
  28.          if(records[name].includes(meal)){
  29.            let indMeal = records[name].indexOf(meal);
  30.            records[name].splice(indMeal,1);
  31.            console.log(`${name} doesn't like the ${meal}.`);
  32.           unlikedMeals++;
  33.         }else{
  34.           console.log(`${name} doesn't have the ${meal} in his/her collection.`)
  35.          }
  36.  
  37.         }else{
  38.           console.log(`${name} is not at the party.`)
  39.         }
  40.  
  41.       ;break;
  42.     }
  43.   }
  44.   let arr = Object.entries(records);
  45.   let comparator = (a,b) => b[1].length - a[1].length ||
  46.   a[0].localeCompare(b[0]);
  47.   arr = arr.sort(comparator);
  48.   arr.forEach(el=>{
  49.     console.log(`${el[0]}: ${el[1].join(', ')}`)
  50.   })
  51.   console.log(`Unliked meals: ${unlikedMeals}`)
  52. }
  53.  
  54. // nikulden([
  55. //   'Like-Krisi-shrimps',
  56. //   'Like-Krisi-soup',
  57. //   'Like-Misho-salad',
  58. //   'Like-Pena-dessert',
  59. //   'Stop'
  60.  
  61. // ]);
  62.  
  63. nikulden([
  64.   "Like-Krisi-shrimps",
  65.   "Unlike-Vili-carp",
  66.   "Unlike-Krisi-salad",
  67.   "Unlike-Krisi-shrimps",
  68.   "Stop"
  69.  
  70. ])
Advertisement
Add Comment
Please, Sign In to add comment