Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function archeryTournament(input) {
- let archery = input.shift().split('|').map(Number);
- let points = 0;
- for (let line of input) {
- let commandLine = line.split('@')
- let command = commandLine[0];
- let startIndex = Number(commandLine[1]);
- let length = Number(commandLine[2]);
- let targetIndex = null;
- if (command === 'Game Over') break;
- else if (command === 'Reverse') archery.reverse();
- else if (command === 'Shoot Left') {
- shootLeftFindTargetIndex(archery);
- calculatePoints(archery, targetIndex);
- } else if (command === 'Shoot Right') {
- shootRightFindTargetIndex(archery);
- calculatePoints(archery, targetIndex);
- }
- function shootLeftFindTargetIndex(array) {
- if (startIndex >= 0 && startIndex < array.length) {
- targetIndex = startIndex - length;
- while (targetIndex < 0) {
- targetIndex = targetIndex + archery.length;
- }
- }
- return targetIndex;
- }
- function shootRightFindTargetIndex(array) {
- if (startIndex >= 0 && startIndex < array.length) {
- targetIndex = startIndex + length;
- while (targetIndex >= array.length) {
- targetIndex = targetIndex - array.length;
- }
- }
- return targetIndex;
- }
- function calculatePoints(array, index) {
- if (array[index] >= 5) {
- points += 5;
- array[index] -= 5;
- } else if (array[index] > 0) {
- points += array[index];
- array[index] = 0;
- }
- }
- }
- console.log(archery.join(' - '));
- console.log(`Iskren finished the archery tournament with ${points} points!`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement