Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let racers = arr[0].split(' ');
- for (let i = 1; i < arr.length; i++) {
- let [cmd, currRacer] = arr[i].split(' ');
- if (cmd === 'Join') {
- if (!racers.includes(currRacer)) {
- racers.push(currRacer);
- }
- } else if (cmd === 'Crash') {
- if (racers.includes(currRacer)) {
- let racerIndex = racers.indexOf(currRacer);
- racers.splice(racerIndex, 1);
- }
- } else if (cmd === 'Pit') {
- if (racers.includes(currRacer)) {
- let racerIndex = racers.indexOf(currRacer);
- if (racerIndex + 1 < racers.length) {
- racers.splice(racerIndex, 1);
- racers.splice(racerIndex + 1, 0, currRacer);
- }
- }
- } else if (cmd === 'Overtake') {
- if (racers.includes(currRacer)) {
- let racerIndex = racers.indexOf(currRacer);
- if (racerIndex - 1 >= 0) {
- racers.splice(racerIndex, 1);
- racers.splice(racerIndex - 1, 0, currRacer);
- }
- }
- }
- }
- console.log(racers.join(' ~ '));
- }
- solve(["Vetel Hamilton Raikonnen Botas Slavi",
- "Pit Hamilton",
- "Overtake LeClerc",
- "Join Ricardo",
- "Crash Botas",
- "Overtake Ricardo",
- "Overtake Ricardo",
- "Overtake Ricardo",
- "Crash Slavi"]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement