svephoto

World Tour [JavaScript]

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