desito07

Inbox Manager

Jul 29th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let inbox = {};    
  3.     for(let i = 0; i < input.length; i++){
  4.         let [command, username, email] = input[i].split('->');
  5.     // console.log(input);
  6.         switch(command){
  7.             case "Add":
  8.                 if(!inbox.hasOwnProperty(username)){
  9.                     inbox[username] = [];
  10.                     // console.log(inbox);                    
  11.             } else {
  12.                 console.log(`${username} is already registered`);                
  13.             }
  14.             break;
  15.             case "Send":
  16.                 if(inbox.hasOwnProperty(username)){
  17.                     inbox[username].push(email);
  18.                 }
  19.                 // console.log(inbox);
  20.                 break;
  21.             case "Delete":
  22.                 if(inbox.hasOwnProperty(username)){
  23.                 delete inbox[username];
  24.                 } else {
  25.                     console.log(`${username} not found!`);
  26.                 }
  27.                 break;
  28.             case "Statistics":
  29.                 break;
  30.         }
  31.     }
  32.     let sortedByEmail = Object.entries(inbox).sort((a, b) => b[1] - a[1]);
  33.     // console.log(sortedByEmail);  
  34.     let sortedByUsername = sortedByEmail.sort((a, b) => a[0].localeCompare(b[0]));
  35.     // console.log(sortedByUsername);
  36.     console.log(`Users count: ${sortedByUsername.length}`);
  37.     for(let kvp of sortedByUsername){
  38.         console.log(`${kvp[0]}`);
  39.             for(let kkk of kvp[1]){
  40.                 console.log(` - ${kkk}`);
  41.             }
  42.     }    
  43. }
Add Comment
Please, Sign In to add comment