Advertisement
Guest User

Untitled

a guest
Aug 14th, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function worldTour(array) {
  2. let i = 0;
  3. let destinations = array[i];
  4. i++;
  5. while (array[i] != "Travel") {
  6. if (array[i].includes("Add")) {
  7. let command = array[i].replace("Add ", "");
  8. let commandSplit = command.split(":");
  9. let startIndex = Number(commandSplit[1]);
  10. if (startIndex < destinations.length && startIndex > 0) {
  11. destinations = destinations.substring(0, startIndex) + commandSplit[2] + destinations.substring(startIndex);
  12. console.log(destinations);
  13. }
  14. } else if (array[i].includes("Remove")) {
  15. let command = array[i].replace("Remove ", "");
  16. let commandSplit = command.split(":");
  17. let startIndex = Number(commandSplit[1]);
  18. let endIndex = Number(commandSplit[2]);
  19. if (startIndex >= 0 && startIndex <= destinations.length && endIndex >= 0 && endIndex <= destinations.length) {
  20. destinations = destinations.slice(0, startIndex) + destinations.slice(endIndex + 1);
  21. console.log(destinations);
  22. }
  23. } else if (array[i].includes("Switch")) {
  24. let commandSplit = array[i].split(":");
  25. let regEx = new RegExp(commandSplit[1], "g");
  26. destinations = destinations.replace(regEx, commandSplit[2])
  27. console.log(destinations);
  28. }
  29. i++;
  30. }
  31. console.log(`Ready for world tour! Planned stops: ${destinations}`);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement