Advertisement
Guest User

Untitled

a guest
Sep 13th, 2020
556
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.  
  5.     while (line !== "Travel") {
  6.         let [command, ...rest] = line.split(":");
  7.  
  8.         switch (command) {
  9.             case "Add Stop":
  10.                 let index = Number(rest[0]);
  11.                 let string = rest[1];
  12.                 if (index >= 0 && index <= allLocations.length) {
  13.                     allLocations = allLocations.split("");
  14.                     allLocations.splice(index, 0, string);
  15.                     allLocations = allLocations.join("");
  16.                 }
  17.                 break;
  18.             case "Remove Stop":
  19.                 let startIndex = Number(rest[0]);
  20.                 let endIndex = Number(rest[1]);
  21.                 if ((startIndex >= 0 && startIndex <= allLocations.length) && (endIndex >= 0 && endIndex <= allLocations.length)) {
  22.                     allLocations = allLocations.split("");
  23.                     allLocations.splice(startIndex, endIndex - startIndex + 1);
  24.                     allLocations = allLocations.join("");
  25.                 }
  26.                 break;
  27.  
  28.             case "Switch":
  29.                 let oldString = rest[0];
  30.                 let newString = rest[1];
  31.                 let rgx = new RegExp(oldString, "g");
  32.                 allLocations = allLocations.replace(rgx, newString);
  33.  
  34.                 break;
  35.         }
  36.         console.log(allLocations);
  37.         line = input.shift();
  38.     }
  39.     console.log(`Ready for world tour! Planned stops: ${allLocations}`);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement