Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pirates(input) {
- const cities = {};
- let city = input.shift();
- while (city !== 'Sail') {
- const [name, population, gold] = city.split('||');
- if (!cities.hasOwnProperty(name)) {
- cities[name] = {
- population: Number(population),
- gold: Number(gold),
- };
- } else {
- cities[name].population += Number(population);
- cities[name].gold += Number(gold);
- }
- city = input.shift();
- }
- let line = input.shift();
- while (line !== 'End') {
- const [command, townName, ...params] = line.split('=>');
- switch (command) {
- case 'Plunder':
- const [citizens, goldAmount] = params.map((x) => Number(x));
- cities[townName].population -= citizens;
- cities[townName].gold -= goldAmount;
- console.log(
- `${townName} plundered! ${goldAmount} gold stolen, ${citizens} citizens killed.`
- );
- if (cities[townName].population <= 0 || cities[townName].gold <= 0) {
- console.log(`${townName} has been wiped off the map!`);
- delete cities[townName];
- }
- break;
- case 'Prosper':
- const [gold] = params.map((x) => Number(x));
- if (gold < 0) {
- console.log('Gold added cannot be a negative number!');
- } else {
- cities[townName].gold += gold;
- console.log(
- `${gold} gold added to the city treasury. ${townName} now has ${cities[townName].gold} gold.`
- );
- }
- break;
- }
- line = input.shift();
- }
- const citiesCount = Object.keys(cities).length;
- if (citiesCount === 0) {
- console.log(
- 'Ahoy, Captain! All targets have been plundered and destroyed!'
- );
- return;
- }
- console.log(
- `Ahoy, Captain! There are ${citiesCount} wealthy settlements to go to:`
- );
- for (const city in cities) {
- console.log(
- `${city} -> Population: ${cities[city].population} citizens, Gold: ${cities[city].gold} kg`
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement