Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pirates(input) {
- let result = {};
- while (input[0] !== 'Sail') {
- let [town, population, gold] = input.shift().split('||')
- population = Number(population);
- gold = Number(gold);
- if (result.hasOwnProperty(town) === false) {
- result[town] = { population, gold }
- } else {
- result[town].population += population;
- result[town].gold += gold;
- }
- }
- let sail = input.shift();
- while (input[0] !== 'End') {
- let tokens = input.shift().split('=>')
- let command = tokens[0];
- if (command === 'Plunder') {
- let city = tokens[1];
- let people = Number(tokens[2]);
- let gold = Number(tokens[3]);
- result[city].population -= people;
- result[city].gold -= gold;
- console.log(`${city} plundered! ${gold} gold stolen, ${people} citizens killed.`);
- if (result[city].population === 0 || result[city].gold === 0) {
- console.log(`${city} has been wiped off the map!`);
- delete result[city];
- }
- } else if (command === 'Prosper') {
- let city = tokens[1];
- let gold = Number(tokens[2]);
- if (gold < 0) {
- console.log(`Gold added cannot be a negative number!`);
- } else {
- result[city].gold += gold;
- console.log(`${gold} gold added to the city treasury. ${city} now has ${result[city].gold} gold.`);
- }
- }
- }
- let str = Object.entries(result);
- if (str.length === 0) {
- console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`);
- } else {
- console.log(`Ahoy, Captain! There are ${str.length} wealthy settlements to go to:`);
- for (let el of str) {
- console.log(`${el[0]} -> Population: ${el[1].population} citizens, Gold: ${el[1].gold} kg`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement