Advertisement
bebo231312312321

Untitled

Mar 22nd, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pirates(input) {
  2.     let firstline = input.splice(0, input.indexOf("Sail"))
  3.     let cities = {}
  4.     let secondLine = input.splice(1, input.indexOf("End") - 1)
  5.     firstline.forEach(element => {
  6.         let [town, population, gold] = element.split("||")
  7.         population = Number(population)
  8.         gold = Number(gold)
  9.         if (!cities.hasOwnProperty(town)) {
  10.             cities[town] = { population, gold }
  11.         } else {
  12.             cities[town]["population"] += population
  13.             cities[town]["gold"] += gold
  14.         }
  15.     });
  16.     secondLine.forEach(element => {
  17.         let [command, city, people, golds] = element.split("=>")
  18.         people = Number(people)
  19.         golds = Number(golds)
  20.  
  21.         switch (command) {
  22.             case "Plunder":
  23.                 console.log(`${city} plundered! ${golds} gold stolen, ${people} citizens killed.`)
  24.                 if (cities.hasOwnProperty(city)) {
  25.                     cities[city]["population"] -= people
  26.                     cities[city]["gold"] -= golds
  27.                 }
  28.                 if (cities[city]["population"] == 0 || cities[city]["gold"] == 0) {
  29.                     delete cities[city]
  30.                     console.log(`${city} has been wiped off the map!`)
  31.                 }
  32.                 break;
  33.             case "Prosper":
  34.                 let goldsLine = people
  35.                 // goldsLine = +goldsLine
  36.                 if (Number(goldsLine) < 0) {
  37.                     console.log(`Gold added cannot be a negative number!`)
  38.                 } else {
  39.                     cities[city]["gold"] += Number(goldsLine)
  40.                     console.log(`${goldsLine} gold added to the city treasury. ${city} now has ${cities[city]["gold"]} gold.`)
  41.                 }
  42.                 break;
  43.  
  44.         }
  45.  
  46.     })
  47.     let count = Object.keys(cities).length
  48.     if (count > 0) {
  49.         console.log(`Ahoy, Captain! There are ${count} wealthy settlements to go to:`)
  50.         for (let keys in cities) {
  51.             console.log(`${keys} -> Population: ${cities[keys]["population"]} citizens, Gold: ${cities[keys]["gold"]} kg`)
  52.         }
  53.     } else {
  54.         console.log("Ahoy, Captain! All targets have been plundered and destroyed!")
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement