Advertisement
Btwonu

Pirates

Aug 8th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. 'use strict';
  2.  
  3. //I basically should've just used a class and defined the methods (commands) on each new town object. I started off with a different solution and had to hack/tweak it to fit my needs so I don't have to refactor the whole thing. I'm setting the 2 methods directly on the town containing object so later on I'm subtracting them from the keys' length so I can print the right amount of towns. I also have a conditional to check if any of the objects' keys are functions and not printing them at the end.
  4. //PS: The commented out sort doesn't work in ALL of the cases. Use the working one from now on.
  5.  
  6. function pirates(arr) {
  7. const cityStats = {};
  8.  
  9. //Loop through targeted cities
  10. let line;
  11. while ((line = arr.shift()) !== 'Sail') {
  12. //Split data from input
  13. let [town, population, gold] = line.split('||');
  14. //Convert to numbers
  15. [population, gold] = [population, gold].map(Number);
  16.  
  17. //If town is already present, just add to existing values
  18. if (cityStats.hasOwnProperty(town)) {
  19. cityStats[town].population += population;
  20. cityStats[town].gold += gold;
  21. } else {
  22. //Create new values
  23. cityStats[town] = {
  24. population,
  25. gold,
  26. };
  27. }
  28. }
  29.  
  30. //Add methods to the obj
  31. cityStats['Plunder'] = plunder;
  32. cityStats['Prosper'] = prosper;
  33.  
  34. //Loop through events
  35. while ((line = arr.shift()) !== 'End') {
  36. //Split data from events
  37. let [command, ...args] = line.split('=>');
  38.  
  39. //Execute command
  40. cityStats[command](args);
  41.  
  42. //Check if town is to be disbanded
  43. let town = args[0];
  44. let deadTown =
  45. cityStats[town].gold === 0 || cityStats[town].population === 0;
  46.  
  47. if (deadTown) {
  48. delete cityStats[town];
  49. console.log(`${town} has been wiped off the map!`);
  50. }
  51. }
  52.  
  53. //Sort
  54. // .sort((townA, townB) => townA.localeCompare(townB))
  55. // .sort((townA, townB) => cityStats[townB].gold - cityStats[townA].gold);
  56. let sortedKeys = Object.keys(cityStats).sort((townA, townB) => {
  57. if (cityStats[townB].gold - cityStats[townA].gold === 0) {
  58. return townA.localeCompare(townB);
  59. } else {
  60. return cityStats[townB].gold - cityStats[townA].gold;
  61. }
  62. });
  63.  
  64. let keysLength = sortedKeys.length - 2 < 0 ? 0 : sortedKeys.length - 2;
  65.  
  66. //Output
  67. console.log(
  68. `Ahoy, Captain! There are ${keysLength} wealthy settlements to go to:`
  69. );
  70.  
  71. sortedKeys.forEach((town) => {
  72. if (typeof cityStats[town] !== 'function') {
  73. console.log(
  74. `${town} -> Population: ${cityStats[town].population} citizens, Gold: ${cityStats[town].gold} kg`
  75. );
  76. }
  77. });
  78.  
  79. //Declarations
  80. function plunder(params) {
  81. let [town, population, gold] = params;
  82. [population, gold] = [population, gold].map(Number);
  83.  
  84. //Subtract plundered gold and killed population
  85. cityStats[town].population -= population;
  86. cityStats[town].gold -= gold;
  87.  
  88. console.log(
  89. `${town} plundered! ${gold} gold stolen, ${population} citizens killed.`
  90. );
  91. }
  92.  
  93. function prosper(params) {
  94. let [town, gold] = params;
  95. gold = Number(gold);
  96.  
  97. if (gold < 0) {
  98. console.log('Gold added cannot be a negative number!');
  99. return;
  100. }
  101.  
  102. //If the gold value is not a negative number add to treasury
  103. cityStats[town].gold += gold;
  104. //Print
  105. console.log(
  106. `${gold} gold added to the city treasury. ${town} now has ${cityStats[town].gold} gold.`
  107. );
  108. }
  109. }
  110.  
  111. let result = pirates([
  112. 'Nassau||95000||1000',
  113. 'San Juan||930000||1250',
  114. 'Campeche||270000||690',
  115. 'Port Royal||320000||1000',
  116. 'Port Royal||100000||2000',
  117. 'Sail',
  118. 'Prosper=>Port Royal=>-200',
  119. 'Plunder=>Nassau=>94000=>750',
  120. 'Plunder=>Nassau=>1000=>150',
  121. 'Plunder=>Campeche=>150000=>690',
  122. 'End',
  123. ]);
  124. console.log(result);
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement