Advertisement
vladovip

JS FUND MIDEX - Man-O-War

Aug 19th, 2022
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOnWar(inputArr) {
  2.   // it gives me 60/100  despite the fact that I have met the answers based on the initial Task explanation.
  3.  
  4.   let pirateShipStatus = inputArr.shift().split(">"); // "12>13>11>20>66",  ->  [ '12', '13', '11', '20', '66' ]
  5.   let warShipStatus = inputArr.shift().split(">"); //  "12>22>33>44>55>32>18",
  6.  
  7.   let maximHealhPerSection = Number(inputArr.shift()); // "70",
  8.   let commandLineInfo = inputArr.shift();
  9.  
  10.   while (commandLineInfo !== "Retire") {
  11.  
  12.     let tokens = commandLineInfo.split(" ");
  13.     let command = tokens[0];
  14.     let param1 = tokens[1];
  15.     let param2 = tokens[2];
  16.     let param3 = tokens[3];
  17.  
  18.     if (command === "Fire") {
  19.       //    "Fire {index} {damage}"
  20.       //     "Fire 2 11",
  21.       let attackIndex = Number(param1);
  22.       let damage = Number(param2);
  23.       if (attackIndex >= 0 && attackIndex < warShipStatus.length) {
  24.         warShipStatus[attackIndex] -= damage;
  25.         if (warShipStatus[attackIndex] <= 0) {
  26.           console.log(`"You won! The enemy ship has sunken.`);
  27.          break;
  28.        }
  29.      }
  30.    }
  31.  
  32.    if (command == "Defend") {
  33.      // "Defend {startIndex} {endIndex} {damage}"
  34.      // "Defend 3 6 11",
  35.      let startindex = Number(param1);
  36.      let endIndex = Number(param2);
  37.      let damage = Number(param3);
  38.      if (
  39.        startindex >= 0 &&
  40.        startindex < pirateShipStatus.length &&
  41.        endIndex >= 0 &&
  42.        endIndex < pirateShipStatus.length &&
  43.        startindex <= endIndex
  44.      ) {
  45.        for (let i = startindex; i <= endIndex; i++) {
  46.          pirateShipStatus[i] -= damage;
  47.        }
  48.      }
  49.      for (let el of pirateShipStatus) {
  50.        if (el <= 0) {
  51.          console.log(`You lost! The pirate ship has sunken.`);
  52.          break;
  53.        }
  54.      }
  55.    }
  56.  
  57.    if (command === "Repair") {
  58.      //  "Repair 1 33",
  59.      //  "Repair {index} {health}"
  60.      let index = Number(param1);
  61.      let health = Number(param2);
  62.      if (index >= 0 && index < pirateShipStatus.length) {
  63.        pirateShipStatus[index] += health;
  64.        if (pirateShipStatus[index] >= maximHealhPerSection) {
  65.          pirateShipStatus[index] = maximHealhPerSection;
  66.        }
  67.      }
  68.    }
  69.  
  70.    if (command == "Status") {
  71.      let counterNeedRepair = 0;
  72.      for (let el of pirateShipStatus) {
  73.        if (el < 0.2 * maximHealhPerSection) {
  74.          counterNeedRepair++;
  75.        }
  76.      }
  77.      console.log(`${counterNeedRepair} sections need repair.`);
  78.    }
  79.  
  80.    commandLineInfo = inputArr.shift();
  81.  }
  82.  
  83.  let pirateSum = 0;
  84.  let warShipSum = 0;
  85.  for (let el of pirateShipStatus) {
  86.    pirateSum += Number(el);
  87.  }
  88.  for (let section of warShipStatus) {
  89.    warShipSum += Number(section);
  90.  }
  91.  
  92.  if (warShipSum > 0 && pirateSum > 0) {
  93.    console.log(`Pirate ship status: ${pirateSum}`);
  94.    console.log(`Warship status: ${warShipSum}`);
  95.  }
  96. }
  97.  
  98.  
  99.  
  100. manOnWar([
  101.  "12>13>11>20>66",
  102.  "12>22>33>44>55>32>18",
  103.  "70",
  104.  "Fire 2 11",
  105.  "Fire 8 100",
  106.  "Defend 3 6 11",
  107.  "Defend 0 3 5",
  108.  "Repair 1 33",
  109.  "Status",
  110.  "Retire",
  111. ]);
  112.  
  113. console.log(`*******`);
  114.  
  115. manOnWar([
  116.  "2>3>4>5>2",
  117.  "6>7>8>9>10>11",
  118.  "20",
  119.  "Status",
  120.  "Fire 2 3",
  121.  "Defend 0 4 11",
  122.  "Repair 3 18",
  123.  "Retire",
  124. ]);
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement