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