Advertisement
dimoBs

world tour

Feb 23rd, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //01. World Tour
  2. function solve(input) {
  3.  
  4.     let tour = input.shift();
  5.     let line = input.shift();
  6.  
  7.     while (line !== 'Travel') {
  8.  
  9.         let [command, ...rest] = line.split(':');
  10.         command = command.trim()
  11.  
  12.         if (command === "Add Stop") {
  13.             let index = Number(rest[0]);
  14.             let string = rest[1];
  15.             if (index >= 0 && index <= tour.length) {
  16.                 tour = tour.split('');
  17.                 tour.splice(index, 0, string);
  18.                 tour = tour.join('');
  19.             }
  20.         } else if (command === "Remove Stop") {
  21.             let strIndex = Number(rest[0]);
  22.             let endIndex = Number(rest[1]);
  23.             if (strIndex >= 0 && strIndex < tour.length &&
  24.                 endIndex >= 0 && endIndex < tour.length) {
  25.                 tour = tour.split('');
  26.                 tour.splice(strIndex, endIndex - strIndex + 1);
  27.                 tour = tour.join('');
  28.             }
  29.         } else if (command === "Switch") {
  30.             let oldString = rest[0];
  31.             let newString = rest[1];
  32.             let rgx = new RegExp(oldString, 'g');
  33.             tour = tour.replace(rgx, newString);
  34.         }
  35.         console.log(tour);
  36.         line = input.shift();
  37.     }
  38.     console.log(`Ready for world tour! Planned stops: ${tour}`)
  39. }
  40. solve(['Hawai::Cyprys-Greece','Add Stop:7:Rome','Remove Stop:11:16','Switch:Hawai:Bulgaria','Travel'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement