Advertisement
vladovip

ManOnWarV2 JS FUND MIDEX

Aug 19th, 2022
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOnWar(input = []) {
  2.  
  3.     let pirateShip = [];
  4.     pirateShip = input.shift().split(`>`).map(Number);
  5.     let warShip = [];
  6.     warShip = input.shift().split(`>`).map(Number);
  7.     let maxHealth = Number(input.shift());
  8.     let token = input.shift();
  9.     let isDead = false;
  10.    
  11.     while (token != `Retire`) {
  12.       if (isDead === true) {
  13.         break;
  14.       }
  15.       let splittedToken = token.split(` `);
  16.       let comand = splittedToken[0];
  17.       let indexOne = splittedToken[1];
  18.       let indexTwo = splittedToken[2];
  19.       let indexThree = splittedToken[3];
  20.    
  21.       if (comand === `Fire`) {
  22.         fire(indexOne, indexTwo);
  23.       } else if (comand === `Defend`) {
  24.         defend(indexOne, indexTwo, indexThree);
  25.       } else if (comand === `Repair`) {
  26.         repair(indexOne, indexTwo);
  27.       } else if (comand === `Status`) {
  28.         status();
  29.       }
  30.    
  31.       token = input.shift();
  32.     }
  33.    
  34.     if (!isDead) {
  35.       let warShipPoint = warShip.reduce((acc, el) => acc + el, 0);
  36.       let pirateShipPoints = pirateShip.reduce((acc, el) => acc + el, 0);
  37.    
  38.       console.log(`Pirate ship status: ${pirateShipPoints}`);
  39.       console.log(`Warship status: ${warShipPoint}`);
  40.     }
  41.    
  42.     function fire(index, damage) {
  43.       index = Number(index);
  44.       damage = Number(damage);
  45.       if (index >= 0 && index < warShip.length) {
  46.         warShip[index] = warShip[index] - damage;
  47.         if (warShip[index] <= 0) {
  48.           isDead = true;
  49.           console.log(`You won! The enemy ship has sunken.`);
  50.           return;
  51.         }
  52.       }
  53.     }
  54.    
  55.     function defend(startIndex, endIndex, damage) {
  56.       startIndex = Number(startIndex);
  57.       endIndex = Number(endIndex);
  58.       damage = Number(damage);
  59.       if ((startIndex >= 0 && startIndex < pirateShip.length) && (endIndex >= startIndex && endIndex <= pirateShip.length - 1)) {
  60.         for (let i = startIndex; i <= endIndex; i++) {
  61.           pirateShip[i] -= damage;
  62.           if (pirateShip[i] <= 0) {
  63.             isDead = true;
  64.             console.log(`You lost! The pirate ship has sunken.`);
  65.             return;
  66.           }
  67.         }
  68.       }
  69.     }
  70.    
  71.     function repair(index, health) {
  72.       index = Number(index);
  73.       health = Number(health);
  74.       if (index >= 0 && index < pirateShip.length) {
  75.         if (pirateShip[index] + health < maxHealth) {
  76.           pirateShip[index] = pirateShip[index] + health;
  77.         } else {
  78.           pirateShip[index] = maxHealth;
  79.         }
  80.    
  81.       }
  82.     }
  83.    
  84.     function status() {
  85.       let lessThan20Perc = maxHealth * 0.2;
  86.       let coundSections = 0;
  87.       for (let i = 0; i < pirateShip.length; i++) {
  88.         if (pirateShip[i] < lessThan20Perc) {
  89.           coundSections++;
  90.         }
  91.       }
  92.       console.log(`${coundSections} sections need repair.`);
  93.    
  94.     }
  95.    
  96.   }
  97.  
  98.  
  99.  
  100.  
  101. manOnWar([
  102.     "12>13>11>20>66",
  103.     "12>22>33>44>55>32>18",
  104.     "70",
  105.     "Fire 2 11",
  106.     "Fire 8 100",
  107.     "Defend 3 6 11",
  108.     "Defend 0 3 5",
  109.     "Repair 1 33",
  110.     "Status",
  111.     "Retire",
  112.   ]);
  113.  
  114.   console.log(`*******`);
  115.  
  116.   manOnWar([
  117.     "2>3>4>5>2",
  118.     "6>7>8>9>10>11",
  119.     "20",
  120.     "Status",
  121.     "Fire 2 3",
  122.     "Defend 0 4 11",
  123.     "Repair 3 18",
  124.     "Retire",
  125.   ]);
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement