Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function worldTour(array) {
- let start = array.shift();
- for (let i = 0; i < array.length; i++) {
- let command = array[i].split(":");
- let token = command[0];
- if (token === "Add Stop") {
- let startIndex = Number(command[1]);
- let destination = command[2];
- if (startIndex >= 0 && startIndex < start.length) {
- start = start.slice(0, startIndex) + destination + start.slice(startIndex, start.length);
- }
- console.log(start);
- } else if (token === "Remove Stop") {
- let startIndex = Number(command[1]);
- let endIndex = Number(command[2]);
- if (startIndex >= 0 && endIndex < start.length) {
- let split = start.substring(startIndex, endIndex + 1);
- start = start.replace(split, "");
- }
- console.log(start);
- } else if (token === "Switch") {
- let oldString = command[1];
- let newString = command[2];
- if (start.includes(oldString)) {
- start = start.split(oldString).join(newString);
- }
- console.log(start);
- } else if (token === "Travel") {
- console.log(`Ready for world tour! Planned stops: ${start}`);
- }
- }
- }
Add Comment
Please, Sign In to add comment