Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pirates(input){
- let cities=input.splice(0,input.indexOf("Sail"));
- let actions=input.splice(1,input.indexOf("End")-1);
- let obj={};
- for (let city of cities) {
- let [name,population,gold]=city.split("||")
- population=Number(population);
- gold=Number(gold);
- if(!obj.hasOwnProperty(name)){
- obj[name]={population,gold};
- }else {
- obj[name].population+=population;
- obj[name].gold+=gold
- }
- }
- for (let action of actions) {
- let [command,townName,valueOne,valueTwo]=action.split("=>");
- switch(command){
- case "Plunder":
- obj[townName].populaton-=Number(valueOne);
- obj[townName].gold-=Number(valueTwo);
- console.log(`${townName} plundered! ${valueTwo} gold stolen, ${valueOne} citizens killed.`)
- if(obj[townName].population<=0 || obj[townName].gold<=0){
- console.log(`${townName} has been wiped off the map!`)
- delete obj[townName];
- }
- break;
- case "Prosper":
- if(valueOne<0){
- console.log(`Gold added cannot be a negative number!`);
- }else {
- obj[townName].gold+=Number(valueOne);
- console.log(`${valueOne} gold added to the city treasury. ${townName} now has ${obj[townName].gold} gold.`)
- }
- break;
- }
- }
- let townNameSort=Object.entries(obj).sort((a,b)=>a[0].localeCompare(b[0]));
- let goldSort=townNameSort.sort((a,b)=>b[1].gold-a[1].gold);
- if(Object.keys(goldSort).length>0){
- console.log(`Ahoy, Captain! There are ${Object.keys(goldSort).length} wealthy settlements to go to:`)
- }else {
- console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
- }
- for(let kvp of goldSort){
- console.log(`${kvp[0]} -> Population: ${kvp[1].population} citizens, Gold: ${kvp[1].gold} kg`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement