Advertisement
Neri0817

World Tour

Mar 29th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function worldTour(input) {
  2.   let string = input.shift();
  3.  
  4.   while (input[0] != "Travel") {
  5.     let line = input.shift().split(":");
  6.     let command = line.shift();
  7.  
  8.     if (command == "Add Stop") {
  9.       let index = Number(line.shift());
  10.       let str = line.shift();
  11.  
  12.       if (index >= 0 && index <= string.length) {
  13.         let left = string.substr(0, index);
  14.         let right = string.substr(index);
  15.         string = left + str + right;
  16.       }
  17.  
  18.       console.log(string);
  19.     }
  20.  
  21.     if (command == "Remove Stop") {
  22.       let startIndex = Number(line.shift());
  23.       let endIndex = Number(line.shift());
  24.  
  25.       if (
  26.         startIndex >= 0 &&
  27.         startIndex < string.length &&
  28.         endIndex >= 0 &&
  29.         endIndex < string.length &&
  30.         startIndex <= endIndex
  31.       ) {
  32.         let remove = string.slice(startIndex, endIndex + 1);
  33.         string = string.replace(remove, "");
  34.       }
  35.       console.log(string);
  36.     }
  37.  
  38.     if (command == "Switch") {
  39.       let oldStr = line.shift();
  40.       let newStr = line.shift();
  41.       if (string.includes(oldStr)) {
  42.         string = string.split(oldStr).join(newStr);
  43.       }
  44.       console.log(string);
  45.     }
  46.   }
  47.   console.log(`Ready for world tour! Planned stops: ${string}`);
  48. }
  49. worldTour([
  50.   "Hawai::Cyprys-Greece",
  51.   "Add Stop:7:Rome",
  52.   "Remove Stop:11:16",
  53.   "Switch:Hawai:Bulgaria",
  54.   "Travel",
  55. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement