SHARE
TWEET

Inbox Manager

a guest Dec 8th, 2019 147 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let data = {};
  3.     for (let el of input) {
  4.         let cmd = el.split('->');
  5.         switch(cmd[0]){
  6.             case 'Add':
  7.                 if (!data.hasOwnProperty(cmd[1])) {
  8.                     data[cmd[1]] = {
  9.                         sent: 0,
  10.                         msgs: []
  11.                     };
  12.                 } else {
  13.                     console.log(`${cmd[1]} is already registered`);
  14.                 }
  15.                 break;
  16.             case 'Send':
  17.                 if (data[cmd[1]]){
  18.                     data[cmd[1]].sent++;
  19.                     data[cmd[1]].msgs.push(cmd[2]);
  20.                 }
  21.                 break;
  22.             case 'Delete':
  23.                 if (data[cmd[1]]) {
  24.                     delete data[cmd[1]];
  25.                 } else {
  26.                     console.log(`${cmd[1]} not found!`);
  27.                 }
  28.                 break;
  29.             case 'Statistics':
  30.                 let arr = Object.entries(data);
  31.                 console.log(`Users count: ${arr.length}`);
  32.                 let cmp = (a, b) => b[1].sent - a[1].sent || a[0].localeCompare(b[0]);
  33.                 arr = arr.sort(cmp);
  34.                 arr.forEach(i => {
  35.                     console.log(`${i[0]}`);
  36.                     i[1].msgs.forEach(i => console.log(`- ${i}`));
  37.                 });
  38.                 break;
  39.         }
  40.     }
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top