Advertisement
TZinovieva

World Tour JS Fundamentals

Mar 30th, 2023
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function worldTour(text) {
  2.  
  3.   let stops = text.shift();
  4.  
  5.   while (text[0] !== "Travel") {
  6.     let tokens = text.shift().split(`:`);
  7.     let command = tokens[0];
  8.  
  9.     if (command == "Add Stop") {
  10.       let index = +tokens[1];
  11.       let string = tokens[2];
  12.       if (index >= 0 && index <= stops.length) {
  13.         let first = stops.substring(0, index);
  14.         let third = stops.substring(index);
  15.         stops = first + string + third;
  16.       }
  17.       console.log(stops);
  18.     } else if (command == `Remove Stop`) {
  19.       let startIndex = +tokens[1];
  20.       let endIndex = +tokens[2];
  21.       if (
  22.         startIndex >= 0 &&
  23.         startIndex < stops.length &&
  24.         endIndex >= 0 &&
  25.         endIndex < stops.length &&
  26.         startIndex <= endIndex
  27.       ) {
  28.         let first = stops.substring(0, startIndex);
  29.         let removedPart = stops.substring(endIndex + 1);
  30.         stops = first + removedPart;
  31.       }
  32.       console.log(stops);
  33.     } else if (command == `Switch`) {
  34.       let oldString = tokens[1];
  35.       let newString = tokens[2];
  36.       if (stops.includes(oldString)) {
  37.         stops = stops.replace(oldString, newString);
  38.         console.log(stops);
  39.       } else {
  40.         console.log(stops);
  41.       }
  42.     }
  43.   }
  44.   console.log(`Ready for world tour! Planned stops: ${stops}`);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement