Advertisement
Liliana797979

archery tournament - mid exam

Aug 15th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let field = input.shift().split('|').map(Number);
  3.     let points = 0;
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.         let splitted = input[i].split('@');
  7.         let command = splitted[0];
  8.         let commandIndex = Number(splitted[1]);
  9.         let length = Number(splitted[2]);
  10.  
  11.         switch (command) {
  12.             case 'Shoot Left':
  13.                 if (commandIndex >= 0 && commandIndex <= field.length - 1) {
  14.                     let fieldIndex = (commandIndex - length) % field.length;
  15.                     if (fieldIndex < 0) {
  16.                         fieldIndex = field.length + fieldIndex;
  17.                     }
  18.                     if (field[fieldIndex] >= 5) {
  19.                         points += 5;
  20.                         field[fieldIndex] -= 5;
  21.                     } else {
  22.                         points += field[fieldIndex];
  23.                         field[fieldIndex] = 0;
  24.                     }
  25.                 }
  26.                 break;
  27.  
  28.             case 'Shoot Right':
  29.                 if (commandIndex >= 0 && commandIndex <= field.length - 1) {
  30.                     let fieldIndex = (commandIndex + length) % field.length;
  31.                     if (field[fieldIndex] >= 5) {
  32.                         points += 5;
  33.                         field[fieldIndex] -= 5;
  34.                     } else {
  35.                         points += field[fieldIndex];
  36.                         field[fieldIndex] = 0;
  37.                     }
  38.                 }
  39.                 break;
  40.  
  41.             case 'Reverse':
  42.                 let reversed = field.reverse();
  43.                 field === reversed;
  44.                 break;
  45.  
  46.             case 'Game over':
  47.                 console.log(field.join(' - '));
  48.                 console.log(`Iskren finished the archery tournament with ${points} points!`);
  49.                 return;
  50.         }
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement