Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function archeryTournament(input) {
  2.  
  3.     let archery = input.shift().split('|').map(Number);
  4.     let points = 0;
  5.    
  6.     for (let line of input) {
  7.         let commandLine = line.split('@')
  8.         let command = commandLine[0];
  9.         let startIndex = Number(commandLine[1]);
  10.         let length = Number(commandLine[2]);
  11.         let targetIndex = null;
  12.        
  13.         if (command === 'Game Over') break;
  14.         else if (command === 'Reverse') archery.reverse();
  15.         else if (command === 'Shoot Left') {
  16.             shootLeftFindTargetIndex(archery);
  17.             calculatePoints(archery, targetIndex);
  18.         } else if (command === 'Shoot Right') {
  19.             shootRightFindTargetIndex(archery);
  20.             calculatePoints(archery, targetIndex);
  21.         }
  22.        
  23.         function shootLeftFindTargetIndex(array) {
  24.             if (startIndex >= 0 && startIndex < array.length) {
  25.                 targetIndex = startIndex - length;
  26.                 while (targetIndex < 0) {
  27.                     targetIndex = targetIndex + archery.length;
  28.                 }
  29.             }
  30.             return targetIndex;
  31.         }
  32.        
  33.         function shootRightFindTargetIndex(array) {
  34.             if (startIndex >= 0 && startIndex < array.length) {
  35.                 targetIndex = startIndex + length;
  36.                 while (targetIndex >= array.length) {
  37.                     targetIndex = targetIndex - array.length;
  38.                 }
  39.             }
  40.             return targetIndex;
  41.         }
  42.        
  43.         function calculatePoints(array, index) {
  44.             if (array[index] >= 5) {
  45.                 points += 5;
  46.                 array[index] -= 5;
  47.             } else if (array[index] > 0) {
  48.                 points += array[index];
  49.                 array[index] = 0;
  50.             }
  51.         }    
  52.     }
  53.     console.log(archery.join(' - '));
  54.     console.log(`Iskren finished the archery tournament with ${points} points!`);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement