Advertisement
divanov94

Untitled

Aug 9th, 2020
55
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.     for(let line of input){
  4.         if(line==="Travel"){
  5.             break;
  6.         }
  7.         let [command,valueOne,valueTwo]=line.split(":");
  8.         switch(command){
  9.             case "Add Stop":
  10.                 let index=Number(valueOne);
  11.                 let toInsert=valueTwo;
  12.  
  13.                 if(index>=0 && index<string.length){
  14.                     let firstPart=string.substring(0,index);
  15.                     let secondPart=string.substring(index);
  16.                     string=firstPart+toInsert+secondPart;
  17.                     console.log(string)
  18.  
  19.                 }
  20.                 break;
  21.             case "Remove Stop":
  22.                 let startInd=Number(valueOne);
  23.                 let endInd=Number(valueTwo);
  24.                 if(startInd>=0 && endInd>=startInd && endInd<string.length){
  25.                     let firstPart=string.substring(0,startInd);
  26.                     let secondPart=string.substring(endInd+1);
  27.                     string=firstPart+secondPart;
  28.                     console.log(string)
  29.  
  30.                 }
  31.                 break;
  32.             case "Switch":
  33.                 let old=valueOne;
  34.                 let newStr=valueTwo;
  35.                 while(string.includes(old)){
  36.                     string=string.replace(old,newStr);
  37.                 }
  38.                 console.log(string)
  39.                 break;
  40.         }
  41.     }
  42.     console.log(`Ready for world tour! Planned stops: ${string}`)
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement