Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr) {
- let pirShipStatus = arr[0].split('>').map(Number);
- let warShipStatus = arr[1].split('>').map(Number);
- let maxHealth = Number(arr[2]);
- arr.splice(0, 3);
- let indexWhile = 0;
- // console.log(arr);
- while (arr[indexWhile] !== 'Retire') {
- let currentCommand = arr[indexWhile].split(' ');
- if (currentCommand[0] === 'Fire') {
- let index = currentCommand[1];
- let damage = currentCommand[2];
- if (Number(index) in warShipStatus) {
- warShipStatus[index] -= Number(damage);
- if (warShipStatus[index] <= 0) {
- return `You won! The enemy ship has sunken.`;
- }
- }
- } else if (currentCommand[0] === 'Defend') {
- let startIndex = currentCommand[1];
- let endIndex = currentCommand[2];
- let damage = currentCommand[3];
- if (Number(startIndex) in pirShipStatus && Number(endIndex) in pirShipStatus) {
- for (let i = startIndex; i <= endIndex; i++) {
- pirShipStatus[i] -= Number(damage);
- if (pirShipStatus[i] <= 0) {
- return `You lost! The pirate ship has sunken.`;
- }
- }
- }
- } else if (currentCommand[0] === 'Repair') {
- let index = currentCommand[1];
- let health = Number(currentCommand[2]);
- if (Number(index) in pirShipStatus) {
- pirShipStatus[index] += health;
- if (pirShipStatus[index] > maxHealth) {
- pirShipStatus[index] = maxHealth;
- }
- }
- } else if (currentCommand[0] === 'Status') {
- let twentyPercent = maxHealth * 0.2;
- let countOfSections = 0;
- pirShipStatus.forEach(par => par < twentyPercent ? countOfSections += 1 : false);
- console.log(`${countOfSections} sections need repair.`);
- }
- indexWhile++;
- }
- let pirateShipSum = 0;
- let warShipSum = 0;
- pirShipStatus.map(par => pirateShipSum += par);
- warShipStatus.map(par => warShipSum += par);
- console.log(`Pirate ship status: ${pirateShipSum}`);
- console.log(`Warship status: ${warShipSum}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement