Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let guests = {};
- let unlikeCount = 0;
- for (const line of input) {
- if(line === 'Stop'){
- break;
- }
- let [reaction, name, meal] = line.split('-');
- switch(reaction){
- case 'Like':
- if(!guests.hasOwnProperty(name)){
- guests[name] = [];
- guests[name].push(meal);
- }else{
- if(!guests[name].includes(meal)){
- guests[name].push(meal);
- }
- }
- break;
- case 'Unlike':
- if(!guests.hasOwnProperty(name)){
- console.log(`${name} is not at the party.`);
- }else{
- let index = guests[name].indexOf(meal);
- if(index > -1){
- guests[name].splice(index,1);
- console.log(`${name} doesn't like the ${meal}.`);
- unlikeCount++;
- }else{
- console.log(`${name} doesn't have the ${meal} in his/her collection.`);
- }
- }
- break;
- }
- }
- Object.entries(guests)
- .sort((a,b)=>{
- if(b[1].length === a[1].length){
- return a[0].localeCompare(b[0]);
- }
- return b[1].length - a[1].length;
- }).forEach(pair=>{
- let name = pair[0];
- let list = pair[1]
- console.log(`${name}: ${list.join(', ')}`);
- });
- console.log(`Unliked meals: ${unlikeCount}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment