Advertisement
pacho_the_python

Untitled

Apr 20th, 2023
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function horse_racing(data) {
  2.     let horses = data.shift().split("|");
  3.     data.pop()
  4.  
  5.     for (const line of data) {
  6.         let command_data = line.split(' ')
  7.         let command = command_data[0]
  8.         if (command === "Retake") {
  9.             let first_horse = command_data[1]
  10.             let second_horse = command_data[2]
  11.             if (horses.includes(first_horse) && horses.includes(second_horse)) {
  12.                 let first_index = horses.indexOf(first_horse);
  13.                 let second_index = horses.indexOf(second_horse);
  14.                 if (first_index < second_index) {
  15.                     let overtaking = horses[first_index];
  16.                     horses[first_index] = horses[second_index];
  17.                     horses[second_index] = overtaking;
  18.                     console.log(`${first_horse} retakes ${second_horse}.`);
  19.                 }
  20.             }
  21.         } else if (command === "Trouble") {
  22.             let first_horse = command_data[1]
  23.             if (horses.includes(first_horse)) {
  24.                 let horse_index = horses.indexOf(first_horse);
  25.                 if (horse_index > 0) {
  26.                     let deleted_horse = horses.splice(horse_index, 1)[0];
  27.                     horses.splice(horse_index - 1, 0, deleted_horse);
  28.                     console.log(`Trouble for ${first_horse} - drops one position.`);
  29.                 }
  30.             }
  31.         } else if (command === "Rage") {
  32.             let firs_horse = command_data[1]
  33.             let index = horses.indexOf(firs_horse);
  34.  
  35.             if (index === horses.length - 1) {
  36.                 console.log(`${firs_horse} rages 2 positions ahead.`);
  37.             }
  38.             else if (index === horses.length - 2) {
  39.                 let deleted_horse = horses.splice(index, 1)[0];
  40.                 horses.splice(index + 1, 0, deleted_horse);
  41.                 console.log(`${firs_horse} rages 2 positions ahead.`);
  42.             }
  43.             else if (index < horses.length - 2) {
  44.                 let deleted_horse = horses.splice(index, 1)[0];
  45.                 horses.splice(index + 2, 0, deleted_horse);
  46.                 console.log(`${firs_horse} rages 2 positions ahead.`);
  47.             }
  48.         } else if (command === "Miracle") {
  49.             let deleted_horse = horses.splice(0, 1)[0];
  50.             horses.splice(horses.length, 0, deleted_horse);
  51.             console.log(`What a miracle - ${deleted_horse} becomes first.`);
  52.         }
  53.     }
  54.     console.log(horses.join('->'));
  55.     console.log(`The winner is: ${horses[horses.length - 1]}`);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement