Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- //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.
- //PS: The commented out sort doesn't work in ALL of the cases. Use the working one from now on.
- function pirates(arr) {
- const cityStats = {};
- //Loop through targeted cities
- let line;
- while ((line = arr.shift()) !== 'Sail') {
- //Split data from input
- let [town, population, gold] = line.split('||');
- //Convert to numbers
- [population, gold] = [population, gold].map(Number);
- //If town is already present, just add to existing values
- if (cityStats.hasOwnProperty(town)) {
- cityStats[town].population += population;
- cityStats[town].gold += gold;
- } else {
- //Create new values
- cityStats[town] = {
- population,
- gold,
- };
- }
- }
- //Add methods to the obj
- cityStats['Plunder'] = plunder;
- cityStats['Prosper'] = prosper;
- //Loop through events
- while ((line = arr.shift()) !== 'End') {
- //Split data from events
- let [command, ...args] = line.split('=>');
- //Execute command
- cityStats[command](args);
- //Check if town is to be disbanded
- let town = args[0];
- let deadTown =
- cityStats[town].gold === 0 || cityStats[town].population === 0;
- if (deadTown) {
- delete cityStats[town];
- console.log(`${town} has been wiped off the map!`);
- }
- }
- //Sort
- // .sort((townA, townB) => townA.localeCompare(townB))
- // .sort((townA, townB) => cityStats[townB].gold - cityStats[townA].gold);
- let sortedKeys = Object.keys(cityStats).sort((townA, townB) => {
- if (cityStats[townB].gold - cityStats[townA].gold === 0) {
- return townA.localeCompare(townB);
- } else {
- return cityStats[townB].gold - cityStats[townA].gold;
- }
- });
- let keysLength = sortedKeys.length - 2 < 0 ? 0 : sortedKeys.length - 2;
- //Output
- console.log(
- `Ahoy, Captain! There are ${keysLength} wealthy settlements to go to:`
- );
- sortedKeys.forEach((town) => {
- if (typeof cityStats[town] !== 'function') {
- console.log(
- `${town} -> Population: ${cityStats[town].population} citizens, Gold: ${cityStats[town].gold} kg`
- );
- }
- });
- //Declarations
- function plunder(params) {
- let [town, population, gold] = params;
- [population, gold] = [population, gold].map(Number);
- //Subtract plundered gold and killed population
- cityStats[town].population -= population;
- cityStats[town].gold -= gold;
- console.log(
- `${town} plundered! ${gold} gold stolen, ${population} citizens killed.`
- );
- }
- function prosper(params) {
- let [town, gold] = params;
- gold = Number(gold);
- if (gold < 0) {
- console.log('Gold added cannot be a negative number!');
- return;
- }
- //If the gold value is not a negative number add to treasury
- cityStats[town].gold += gold;
- //Print
- console.log(
- `${gold} gold added to the city treasury. ${town} now has ${cityStats[town].gold} gold.`
- );
- }
- }
- let result = pirates([
- 'Nassau||95000||1000',
- 'San Juan||930000||1250',
- 'Campeche||270000||690',
- 'Port Royal||320000||1000',
- 'Port Royal||100000||2000',
- 'Sail',
- 'Prosper=>Port Royal=>-200',
- 'Plunder=>Nassau=>94000=>750',
- 'Plunder=>Nassau=>1000=>150',
- 'Plunder=>Campeche=>150000=>690',
- 'End',
- ]);
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement