Advertisement
ErolKZ

Untitled

Oct 21st, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. function solve (arr) {
  2.  
  3.  
  4. let pirShipStatus = arr[0].split('>').map(Number);
  5.  
  6. let warShipStatus = arr[1].split('>').map(Number);
  7.  
  8. let maxHealth = Number(arr[2]);
  9.  
  10. arr.splice(0, 3);
  11.  
  12.  
  13. let indexWhile = 0;
  14.  
  15. // console.log(arr);
  16.  
  17.  
  18.  
  19. while (arr[indexWhile] !== 'Retire') {
  20.  
  21.  
  22.  
  23.  
  24. let currentCommand = arr[indexWhile].split(' ');
  25.  
  26.  
  27. if (currentCommand[0] === 'Fire') {
  28.  
  29. let index = currentCommand[1];
  30.  
  31. let damage = currentCommand[2];
  32.  
  33. if (Number(index) in warShipStatus) {
  34.  
  35. warShipStatus[index] -= Number(damage);
  36.  
  37. if (warShipStatus[index] <= 0) {
  38.  
  39. return `You won! The enemy ship has sunken.`;
  40.  
  41. }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. } else if (currentCommand[0] === 'Defend') {
  48.  
  49. let startIndex = currentCommand[1];
  50.  
  51. let endIndex = currentCommand[2];
  52.  
  53. let damage = currentCommand[3];
  54.  
  55.  
  56. if (Number(startIndex) in pirShipStatus && Number(endIndex) in pirShipStatus) {
  57.  
  58.  
  59. for (let i = startIndex; i <= endIndex; i++) {
  60.  
  61. pirShipStatus[i] -= Number(damage);
  62.  
  63. if (pirShipStatus[i] <= 0) {
  64.  
  65. return `You lost! The pirate ship has sunken.`;
  66.  
  67. }
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77.  
  78. } else if (currentCommand[0] === 'Repair') {
  79.  
  80. let index = currentCommand[1];
  81.  
  82. let health = Number(currentCommand[2]);
  83.  
  84.  
  85. if (Number(index) in pirShipStatus) {
  86.  
  87.  
  88. pirShipStatus[index] += health;
  89.  
  90. if (pirShipStatus[index] > maxHealth) {
  91.  
  92. pirShipStatus[index] = maxHealth;
  93.  
  94. }
  95.  
  96.  
  97. }
  98.  
  99.  
  100. } else if (currentCommand[0] === 'Status') {
  101.  
  102. let twentyPercent = maxHealth * 0.2;
  103.  
  104. let countOfSections = 0;
  105.  
  106. pirShipStatus.forEach(par => par < twentyPercent ? countOfSections += 1 : false);
  107.  
  108.  
  109. console.log(`${countOfSections} sections need repair.`);
  110.  
  111.  
  112. }
  113.  
  114. indexWhile++;
  115.  
  116. }
  117.  
  118.  
  119. let pirateShipSum = 0;
  120.  
  121. let warShipSum = 0;
  122.  
  123.  
  124. pirShipStatus.map(par => pirateShipSum += par);
  125.  
  126. warShipStatus.map(par => warShipSum += par);
  127.  
  128.  
  129. console.log(`Pirate ship status: ${pirateShipSum}`);
  130.  
  131. console.log(`Warship status: ${warShipSum}`);
  132.  
  133.  
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement