Advertisement
divanov94

Untitled

Jul 31st, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pirates(input){
  2.     let cities=input.splice(0,input.indexOf("Sail"));
  3.     let actions=input.splice(1,input.indexOf("End")-1);
  4.     let obj={};
  5.     for (let city of cities) {
  6.         let [name,population,gold]=city.split("||")
  7.         population=Number(population);
  8.         gold=Number(gold);
  9.  
  10.         if(!obj.hasOwnProperty(name)){
  11.             obj[name]={population,gold};
  12.         }else {
  13.             obj[name].population+=population;
  14.             obj[name].gold+=gold
  15.         }
  16.  
  17.     }
  18.     for (let action of actions) {
  19.         let [command,townName,valueOne,valueTwo]=action.split("=>");
  20.  
  21.  
  22.         switch(command){
  23.  
  24.          case "Plunder":
  25.  
  26.  
  27.              obj[townName].populaton-=Number(valueOne);
  28.  
  29.  
  30.              obj[townName].gold-=Number(valueTwo);
  31.              console.log(`${townName} plundered! ${valueTwo} gold stolen, ${valueOne} citizens killed.`)
  32.  
  33.  
  34.              if(obj[townName].population<=0 || obj[townName].gold<=0){
  35.                  console.log(`${townName} has been wiped off the map!`)
  36.                  delete obj[townName];
  37.              }
  38.  
  39.  
  40.  
  41.              break;
  42.  
  43.          case "Prosper":
  44.  
  45.                 if(valueOne<0){
  46.                     console.log(`Gold added cannot be a negative number!`);
  47.                 }else {
  48.                     obj[townName].gold+=Number(valueOne);
  49.                     console.log(`${valueOne} gold added to the city treasury. ${townName} now has ${obj[townName].gold} gold.`)
  50.                 }
  51.  
  52.                 break;
  53.  
  54.         }
  55.  
  56.     }
  57.     let townNameSort=Object.entries(obj).sort((a,b)=>a[0].localeCompare(b[0]));
  58.     let goldSort=townNameSort.sort((a,b)=>b[1].gold-a[1].gold);
  59.     if(Object.keys(goldSort).length>0){
  60.         console.log(`Ahoy, Captain! There are ${Object.keys(goldSort).length} wealthy settlements to go to:`)
  61.     }else {
  62.         console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
  63.     }
  64.  
  65.  
  66.     for(let kvp of goldSort){
  67.         console.log(`${kvp[0]} -> Population: ${kvp[1].population} citizens, Gold: ${kvp[1].gold} kg`)
  68.  
  69.  
  70.  
  71.     }
  72.  
  73.  
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement