Advertisement
drak138

P!rates-Fundamentals-05-Final Exam

Mar 20th, 2024 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Pirates(arr){
  2. let cities={}
  3. //Използваме i за да знаем през колко града сме минали за да можем после да ги премахнем от arr
  4. let i=0
  5. for(let row of arr){
  6.     i++
  7.     //Ако row е равно на Sail излизаме от цикъла
  8.     if(row==="Sail"){
  9.         break;
  10.     }
  11.     //Разделяме row на city,population и gold като row.split("||")= Оutput:row["Tortuga","345000","1250"]
  12.     let[city,population,gold]=row.split("||")
  13.     //Превръщаме популацията и златото в числа
  14.     population=Number(population)
  15.     gold=Number(gold)
  16.     //Ако градът не съществува в обекта cities извърши:
  17.     if(!cities.hasOwnProperty(city)){
  18.        
  19.     //Създаваме обект в обекта с името на града неговата популация и злато    
  20.     cities[city]={cityPopulation: population,
  21.         cityGold: gold}
  22.     //Output:cities{Tortuga:{cityPopulation:345000,cityGold:1250}}
  23.        
  24.     }
  25.     //Ако градът вече съществува в обекта извърши:
  26.     else{
  27.         //Влезни в cities обекта и добави популацията към value на ключа cityPopulation намиращ се в ключа Tortuga
  28.         cities[city].cityPopulation+=population
  29.         //Влезни в cities обекта и добави златото към value на ключа cityGold намиращ се в ключа Tortuga
  30.         cities[city].cityGold+=gold
  31.     }
  32. }
  33. //Изрязваме градовете и sail от arr
  34. arr.splice(0,i)
  35. for(let row of arr){
  36.     //Сплитваме row на command,city,n1 и n2 като row.split("=>")=Оutput:row["Plunder","Tortuga",75000","380"]
  37.     let[command,city,n1,n2]=row.split("=>")
  38.     //Във всеки случай имаме едно число
  39.     n1=Number(n1)
  40.     //Проверяваме дали n2 същестува за да не ни даде грешка че превръщаме undefined в число. Ако n2 съществува извърши:
  41.     if(!isNaN(n2)){
  42.         n2=Number(n2)
  43.     }
  44.     switch(command){
  45.         case "Plunder":
  46.             let peopleKilled=n1
  47.             let goldStolen=n2
  48.             //Извършваме само ако градът от който трябва да крадем същестува в cities
  49.             if(cities.hasOwnProperty(city)){
  50.                 console.log(`${city} plundered! ${goldStolen} gold stolen, ${peopleKilled} citizens killed.`)
  51.                 //Влезни в cities обекта и извади убитите от популацията от value на ключа cityPopulation намиращ се в ключа Tortuga
  52.                 cities[city].cityPopulation-=peopleKilled
  53.                 //Влезни в cities обекта и извади откраднатото злато от златото от value на ключа cityPopulation намиращ се в ключа Tortuga
  54.                 cities[city].cityGold-=goldStolen
  55.                 //Ако хората или златото на града станат по малко или равно на 0 извърши:
  56.                 if(cities[city].cityGold<=0||cities[city].cityPopulation<=0){
  57.                     console.log(`${city} has been wiped off the map!`)
  58.                     //Изтрии ключа Tortuga и неговото value от обекта cities
  59.                     delete cities[city]
  60.                 }
  61.             }
  62.             break;
  63.         case "Prosper":
  64.             let goldGain=n1
  65.             //Ако златото е по малко от 0 извърши:
  66.             if(goldGain<0){
  67.                 console.log("Gold added cannot be a negative number!")
  68.                 break;
  69.             }
  70.             //Извършваме само ако градът които просперира същестува в cities
  71.             if(cities.hasOwnProperty(city)){
  72.                 //Влезни в cities обекта и добави златото към value на ключа cityGold в намиращ се в ключа Tortuga
  73.                 cities[city].cityGold+=goldGain
  74.                 console.log(`${goldGain} gold added to the city treasury. ${city} now has ${cities[city].cityGold} gold.`)
  75.             }
  76.             break;
  77.     }
  78. }
  79.     //Проверяваме дали в обекта има нещо като Object.keys(cities) връща масив от ключове
  80.     //Оutput:["Tortuga","Santo Domingo","Havana"]
  81.     //Ако обекта е празен извърши:
  82. if(Object.keys(cities).length===0){
  83.     console.log("Ahoy, Captain! All targets have been plundered and destroyed!")
  84. }
  85.     //Ако обекта не е празен извърши:
  86. else{
  87.     console.log(`Ahoy, Captain! There are ${Object.keys(cities).length} wealthy settlements to go to:`)
  88.     //Минаваме през ключовете в cities
  89.     //console.log(city)
  90.     //Output:"Tortuga"
  91.     for(let city in cities){
  92.         console.log(`${city} -> Population: ${cities[city].cityPopulation} citizens, Gold: ${cities[city].cityGold} kg`)
  93.     }
  94. }
  95. }
  96. Pirates(["Tortuga||345000||1250",
  97. "Santo Domingo||240000||630",
  98. "Havana||410000||1100",
  99. "Sail",
  100. "Plunder=>Tortuga=>75000=>380",
  101. "Prosper=>Santo Domingo=>180",
  102. "End"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement