Advertisement
Liliana797979

nikulden's meals - viarno reshenie - fundamentals

Aug 14th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let x = input.shift()
  3.     let obj = {}
  4.     let disliked = 0
  5.     while(x !== 'Stop'){
  6.         let [act, name, meal] = x.split('-')
  7.         if(act === 'Unlike'){
  8.             if(!obj.hasOwnProperty(name)){  
  9.                 console.log(`${name} is not at the party.`)
  10.             }
  11.             else{
  12.                 if(obj[name].indexOf(meal) === -1){
  13.                     console.log(`${name} doesn't have the ${meal} in his/her collection.`);
  14.                }
  15.                else{
  16.                    console.log(`${name} doesn't like the ${meal}.`);
  17.                     let index = obj[name].indexOf(meal)
  18.                     obj[name].splice(index, 1)
  19.                     disliked++
  20.                 }
  21.             }
  22.         }
  23.         else if(act === 'Like'){
  24.             if(!obj.hasOwnProperty(name)){
  25.                 obj[name] = []
  26.                 obj[name].push(meal)
  27.             }
  28.             else{
  29.                 if(obj[name].indexOf(meal) === -1){
  30.                     obj[name].push(meal) // тук го добавям само ако го няма в масива и без проверката ми гърмеше тест 7
  31.                 }
  32.             }
  33.         }
  34.  
  35.         x = input.shift()
  36.     }
  37.     let arr = Object.entries(obj)
  38.     arr.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0])) // сортирам първо по length и после по азбучен ред
  39.     // arr.map(x => x[1] = [...new Set(x[1])])  по този начин махах еднаквите meal но ми гърмеше тест 7 (най-вероятно се пренареждат и judge го смята за различно)
  40.     for(let i of arr){
  41.         console.log(`${i[0]}: ${i[1].join(', ')}`);
  42.     }
  43.     console.log(`Unliked meals: ${disliked}`);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement