Advertisement
divanov94

Untitled

Jul 30th, 2020
136
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.         }
  13.  
  14.     }
  15.     for (let action of actions) {
  16.         let [command,townName,valueOne,valueTwo]=action.split("=>");
  17.  
  18.  
  19.         switch(command){
  20.  
  21.          case "Plunder":
  22.  
  23.              obj[townName].populaton-=Number(valueOne);
  24.  
  25.              obj[townName].gold-=Number(valueTwo);
  26.              console.log(`${townName} plundered! ${valueTwo} gold stolen, ${valueOne} citizens killed.`)
  27.  
  28.  
  29.              if(obj[townName].population===0 || obj[townName].gold===0){
  30.                  console.log(`${townName} has been wiped off the map!`)
  31.                  delete obj[townName];
  32.              }
  33.  
  34.  
  35.  
  36.              break;
  37.  
  38.          case "Prosper":
  39.                 if(valueOne<0){
  40.                     console.log(`Gold added cannot be a negative number!`);
  41.                 }else {
  42.                     obj[townName].gold+=Number(valueOne);
  43.                     console.log(`${valueOne} gold added to the city treasury. ${townName} now has ${obj[townName].gold} gold.`)
  44.                 }
  45.  
  46.                 break;
  47.  
  48.         }
  49.  
  50.     }
  51.     let townNameSort=Object.entries(obj).sort((a,b)=>a[0].localeCompare(b[0]));
  52.     let goldSort=townNameSort.sort((a,b)=>b[1].gold-a[1].gold);
  53.     if(Object.keys(goldSort).length>0){
  54.         console.log(`Ahoy, Captain! There are ${Object.keys(goldSort).length} wealthy settlements to go to:`)
  55.     }else {
  56.         console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
  57.     }
  58.  
  59.  
  60.     for(let kvp of goldSort){
  61.         console.log(`${kvp[0]} -> Population: ${kvp[1].population} citizens, Gold: ${kvp[1].gold} kg`)
  62.  
  63.  
  64.  
  65.     }
  66.  
  67.  
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement