Advertisement
Guest User

WorldTour2

a guest
Sep 13th, 2020
841
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 offset = 0;
  35.                 while (allLocations.indexOf(oldString, offset) >= 0) {
  36.                     allLocations = allLocations.replace(oldString, (oldString, offset) => newString);
  37.                     offset = allLocations.indexOf(oldString, offset) + newString.length;
  38.                 }
  39.                
  40.                 break;
  41.             default:
  42.                 break;
  43.         }
  44.         console.log(allLocations);
  45.         line = input.shift();
  46.     }
  47.     console.log(`Ready for world tour! Planned stops: ${allLocations}`);
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement