Advertisement
Guest User

Untitled

a guest
May 29th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let str = input.shift()
  4.  
  5.     const actions = { "Add Stop": addStop, "Remove Stop": remove, Switch: switched }
  6.  
  7.  
  8.  
  9.     function addStop(index, newPart) {
  10.  
  11.         if (str[index] !== undefined) {             //here
  12.  
  13.             index = Number(index)
  14.  
  15.             let firstPart = str.substring(0, index);
  16.  
  17.             let secondPart = str.substring(index);
  18.  
  19.             str = firstPart + newPart + secondPart;
  20.  
  21.         }
  22.  
  23.         console.log(str)
  24.  
  25.     }
  26.  
  27.     function remove(startIndex, endIndex) {
  28.  
  29.         startIndex = Number(startIndex);
  30.  
  31.         endIndex = Number(endIndex);
  32.  
  33.         if (str[startIndex] !== undefined && str[endIndex] !== undefined) {         //here
  34.  
  35.             let cut = str.substring(startIndex, endIndex + 1);
  36.  
  37.             str = str.replace(cut, "")
  38.  
  39.         }
  40.  
  41.         console.log(str)
  42.  
  43.     }
  44.  
  45.     function switched(oldItem, newItem) {
  46.  
  47.         //if (str.includes(oldItem)) {
  48.         //
  49.         //    while (str.includes(oldItem)) {
  50.         //
  51.         //        str = str.replace(oldItem, newItem)
  52.         //
  53.         //    }
  54.         //
  55.         //}
  56.         if (str.includes(oldItem)) {                        //here
  57.             str = str.split(oldItem).join(newItem);         //here
  58.         }                                                   //here
  59.  
  60.         console.log(str)
  61.  
  62.     }
  63.  
  64.     for (let i = 0; i < input.length; i++) {
  65.  
  66.         let [command, a, b] = input[i].split(":")
  67.  
  68.         if (input[i] === "Travel") break
  69.  
  70.         actions[command](a, b)
  71.  
  72.     }
  73.  
  74.     console.log(`Ready for world tour! Planned stops: ${str}`)
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement