georgiev955

plant discovery

Jul 24th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function worldTour(input){
  2.     let stops = input.shift();
  3.     let command = input.shift();
  4.     while(command !== 'Travel') {
  5.         let [action, ...data] = command.split(':');
  6.         if(action.includes('Add')) {
  7.             let [index, string] = data;
  8.             index = Number(index);
  9.             if(index >= 0 && index < stops.length) {
  10.                 stops = stops.slice(0, index) + string + stops.slice(index);
  11.             }
  12.             console.log(stops);
  13.         } else if (action.includes('Remove')) {
  14.             let [startIndex, endIndex] = data.map(Number);
  15.             if(startIndex >= 0 && endIndex >= startIndex && endIndex < stops.length)  {
  16.                 let substring = stops.slice(startIndex, endIndex+1);
  17.                 stops = stops.replace(substring, '');
  18.             }
  19.             console.log(stops);
  20.         } else {
  21.             let [oldString, newString] = data;
  22.             if(stops.includes(oldString)) {
  23.                 let splitted = stops.split(oldString);
  24.                 stops = splitted.join(newString);
  25.             }
  26.             console.log(stops);
  27.         }
  28.         command = input.shift();
  29.     }
  30.     console.log(`Ready for world tour! Planned stops: ${stops}`);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment