Advertisement
ShadowEmbrace

F1 Race

Apr 15th, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let racers = arr[0].split(' ');
  3.  
  4.     for (let i = 1; i < arr.length; i++) {
  5.         let [cmd, currRacer] = arr[i].split(' ');
  6.        
  7.         if (cmd === 'Join') {
  8.             if (!racers.includes(currRacer)) {
  9.                 racers.push(currRacer);
  10.             }
  11.         } else if (cmd === 'Crash') {
  12.             if (racers.includes(currRacer)) {
  13.                
  14.                 let racerIndex = racers.indexOf(currRacer);
  15.                 racers.splice(racerIndex, 1);
  16.             }
  17.         } else if (cmd === 'Pit') {
  18.             if (racers.includes(currRacer)) {
  19.  
  20.                 let racerIndex = racers.indexOf(currRacer);
  21.  
  22.                 if (racerIndex + 1 < racers.length) {
  23.                     racers.splice(racerIndex, 1);
  24.                     racers.splice(racerIndex + 1, 0, currRacer);
  25.                 }
  26.             }
  27.         } else if (cmd === 'Overtake') {
  28.             if (racers.includes(currRacer)) {
  29.  
  30.                 let racerIndex = racers.indexOf(currRacer);
  31.  
  32.                 if (racerIndex - 1 >= 0) {
  33.                     racers.splice(racerIndex, 1);
  34.                     racers.splice(racerIndex - 1, 0, currRacer);
  35.                 }
  36.             }
  37.         }
  38.     }
  39.  
  40.     console.log(racers.join(' ~ '));
  41. }
  42.  
  43. solve(["Vetel Hamilton Raikonnen Botas Slavi",
  44.     "Pit Hamilton",
  45.     "Overtake LeClerc",
  46.     "Join Ricardo",
  47.     "Crash Botas",
  48.     "Overtake Ricardo",
  49.     "Overtake Ricardo",
  50.     "Overtake Ricardo",
  51.     "Crash Slavi"]
  52. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement