Liliana797979

viarno reshenie - world tour - final exam

Aug 4th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function foo(arr) {
  4.     let stops = arr.shift()
  5.     arr = arr.slice(0, arr.indexOf("Travel"))
  6.     const actions = {
  7.         "Add Stop": addStop,
  8.         "Remove Stop": removeStop,
  9.         Switch: switchIt,
  10.     }
  11.     function addStop(i, s) {
  12.         if (stops[i] !== undefined) {
  13.             stops = stops.substring(0, i) + s + stops.substring(i)
  14.         }
  15.     }
  16.  
  17.     function removeStop(x, y) {
  18.         if (stops[x] !== undefined && stops[y] !== undefined) {
  19.             stops = stops.slice(0, x) + stops.slice(y + 1)
  20.         }
  21.     }
  22.  
  23.     function switchIt(a, b) {
  24.         if (stops.indexOf(a) !== -1) {
  25.             b = b.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&")
  26.             const regx = new RegExp(a, "g")
  27.             stops = stops.replace(regx, b)
  28.         }
  29.     }
  30.  
  31.     for (let i = 0; i < arr.length; i++) {
  32.         const [action, x, y] = arr[i]
  33.             .split(":")
  34.             .map(x => (isNaN(x) ? x : Number(x)))
  35.         actions[action](x, y)
  36.         console.log(stops)
  37.     }
  38.  
  39.     console.log(`Ready for world tour! Planned stops: ${stops}`)
  40. }
Advertisement
Add Comment
Please, Sign In to add comment