SHARE
TWEET

Untitled

a guest Apr 15th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params){
  2.     params.pop();
  3.     let stores = {};
  4.     let itemsArray = [];
  5.  
  6.     for ( let param of params){
  7.         let tokens = param.split('->');
  8.         let command = tokens[0];
  9.         let store = tokens[1];
  10.         let items = tokens[2];
  11.  
  12.         if(command === "Add") {
  13.             if (items.includes(',')) {
  14.                 let splittedItems = items.split(',');
  15.                 if(!stores.hasOwnProperty(store)){
  16.                     stores[store] = splittedItems;
  17.                 }else{
  18.                     for ( let ele of splittedItems){
  19.                         stores[store].push(ele);
  20.                     }
  21.                 }
  22.             }else{
  23.                 if(!stores.hasOwnProperty(store)){
  24.                     stores[store] = [items];
  25.                 }else{
  26.                     stores[store].push(items);
  27.                 }
  28.             }
  29.         }
  30.         else if(command === "Remove"){
  31.             if(stores.hasOwnProperty(store)){
  32.                 delete stores[store];
  33.             }
  34.         }
  35.     }
  36.  
  37.     console.log("Stores list:");
  38.     let sortedStores = Object.entries(stores)
  39.         .sort((a,b)=>{
  40.             return b[1].length - a[1].length || b[0].localeCompare([a[0]])
  41.         })
  42.         .forEach(e=>{
  43.             console.log(`${e[0]}`);
  44.             for ( let item of e[1]){
  45.                 console.log(`<<${item}>>`);
  46.             }
  47.         });
  48.  
  49. }
  50. solve(["Add->PeakSports->Map,Navigation,Compass",
  51. "Add->Paragon->Sunscreen" ,
  52. "Add->Groceries->Dried-fruit,Nuts" ,
  53. "Add->Groceries->Nuts" ,
  54. "Add->Paragon->Tent" ,
  55. "Remove->Paragon" ,
  56. "Add->Pharmacy->Pain-killers" ,
  57. "END"]);
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