Advertisement
Guest User

WorldTour

a guest
Sep 13th, 2020
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printPlannedStops(input) {
  2.     let allLocations = input.shift();
  3.     let line = input.shift();
  4.     while (line !== 'Travel') {
  5.         let [command, ...rest] = line.split(':');
  6.         switch (command.trim()) {
  7.             case 'Add Stop':
  8.                 let index = Number(rest[0]);
  9.                 let string = rest[1];
  10.                 if (index >= 0 && index <= allLocations.length) {
  11.                     allLocations = allLocations.split('');
  12.                     allLocations.splice(index, 0, string);
  13.                     allLocations = allLocations.join('');
  14.                 }
  15.                 break;
  16.             case 'Remove Stop':
  17.                 let startIndex = Number(rest[0]);
  18.                 let endIndex = Number(rest[1]);
  19.                 if (
  20.                     startIndex >= 0 &&
  21.                     startIndex < allLocations.length &&
  22.                     endIndex >= 0 &&
  23.                     endIndex < allLocations.length &&
  24.                     startIndex <= endIndex
  25.                 ) {
  26.                     allLocations = allLocations.split('');
  27.                     allLocations.splice(startIndex, endIndex - startIndex + 1);
  28.                     allLocations = allLocations.join('');
  29.                 }
  30.                 break;
  31.             case 'Switch':
  32.                 let oldString = rest[0];
  33.                 let newString = rest[1];              
  34.                 let rgx = new RegExp(oldString, 'g');
  35.                 allLocations = allLocations.replace(rgx, newString);
  36.                 break;
  37.             default:
  38.                 break;
  39.         }
  40.         console.log(allLocations);
  41.         line = input.shift();
  42.     }
  43.     console.log(`Ready for world tour! Planned stops: ${allLocations}`);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement