kstoyanov

03. Inbox Manager js exam

Aug 13th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const users = {};
  3.   let inputLine = args.shift();
  4.  
  5.   while (inputLine !== 'Statistics') {
  6.     const [command, userName, email] = inputLine.split('->');
  7.  
  8.  
  9.     switch (command) {
  10.       case 'Add':
  11.         if (!Object.prototype.hasOwnProperty.call(users, userName)) {
  12.           const emails = [];
  13.           users[userName] = { emails };
  14.         } else {
  15.           console.log(`${userName} is already registered`);
  16.         }
  17.         break;
  18.       case 'Send':
  19.         users[userName].emails.push(email);
  20.         break;
  21.       case 'Delete':
  22.         if (Object.prototype.hasOwnProperty.call(users, userName)) {
  23.           delete users[userName];
  24.         } else {
  25.           console.log(`${userName} not found!`);
  26.         }
  27.         break;
  28.       default:
  29.         break;
  30.     }
  31.  
  32.  
  33.     inputLine = args.shift();
  34.   }
  35.  
  36.   console.log(`Users count: ${Object.keys(users).length}`);
  37.   Object.entries(users)
  38.     .sort((a, b) => b[1].emails.length - a[1].emails.length || a[0].localeCompare(b[0]))
  39.     .forEach((el) => {
  40.       const [name, arg1] = el;
  41.  
  42.       console.log(`${name}`);
  43.       arg1.emails.forEach((email) => {
  44.         console.log(` - ${email}`);
  45.       });
  46.     });
  47. }
Advertisement
Add Comment
Please, Sign In to add comment