Advertisement
Guest User

Untitled

a guest
May 23rd, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.     let pirateShip = input.shift().split('>').map(Number);
  3.     let warShip = input.shift().split('>').map(Number);
  4.     let maxHealth = Number(input.shift());
  5.     let needRepair = 0;
  6.     let command;
  7.  
  8.     while ((command = input.shift()) !== 'Retire') {
  9.  
  10.         let currComand = command.split(' ')
  11.  
  12.         switch (currComand[0]) {
  13.             case 'Fire':
  14.                 let index = +currComand[1];
  15.                 let damage = +currComand[2];
  16.                 if (index <= warShip.length) {
  17.                     let attack = warShip[index] - damage;
  18.                     warShip[index] = attack;
  19.                     if (warShip[index] <= 0) {
  20.                         console.log('You won! The enemy pirateShip has sunken.');
  21.                         return;
  22.                     }
  23.                 }
  24.                 break;
  25.  
  26.             case 'Defend':
  27.                 let startIndex = +currComand[1];
  28.                 let endIndex = +currComand[2];
  29.                 let attackPower = +currComand[3];
  30.  
  31.                 if (startIndex <= pirateShip.length && endIndex <= pirateShip.length) {
  32.                     for (let i = startIndex; i <= endIndex; i++) {
  33.                         pirateShip[i] -= attackPower;
  34.  
  35.                         if (pirateShip[i] <= 0) {
  36.                             console.log(`You lost! The pirate ship has sunken.`);
  37.                             return;
  38.                         }
  39.                     }
  40.                 }
  41.                 break;
  42.             case 'Repair':
  43.                 let repairIndex = +currComand[1];
  44.                 let health = +currComand[2];
  45.                 if (repairIndex <= pirateShip.length) {
  46.                     if (health + pirateShip[repairIndex] <= 100) {
  47.                         pirateShip[repairIndex] += health;
  48.                     }
  49.                 }
  50.                 break;
  51.  
  52.             case 'Status':
  53.                 for (let i = 0; i < pirateShip.length; i++) {
  54.                     const element = pirateShip[i];
  55.                     if (element < maxHealth * 0.20) {
  56.                         needRepair++;
  57.                     }
  58.                 }
  59.                 console.log(`${needRepair} sections need repair.`);
  60.         }
  61.     }
  62.     let sumWarShip = warShip.reduce((a, b) => a + b);
  63.     let sumPirate = pirateShip.reduce((acc, value) => acc + value);
  64.     console.log(`Pirate ship status: ${sumPirate}`);
  65.     console.log(`Warship status: ${sumWarShip}`);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement