Advertisement
Guest User

Untitled

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