Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function worldTour(input) {
- let string = input.shift();
- while (input[0] != "Travel") {
- let line = input.shift().split(":");
- let command = line.shift();
- if (command == "Add Stop") {
- let index = Number(line.shift());
- let str = line.shift();
- if (index >= 0 && index <= string.length) {
- let left = string.substr(0, index);
- let right = string.substr(index);
- string = left + str + right;
- }
- console.log(string);
- }
- if (command == "Remove Stop") {
- let startIndex = Number(line.shift());
- let endIndex = Number(line.shift());
- if (
- startIndex >= 0 &&
- startIndex < string.length &&
- endIndex >= 0 &&
- endIndex < string.length &&
- startIndex <= endIndex
- ) {
- let remove = string.slice(startIndex, endIndex + 1);
- string = string.replace(remove, "");
- }
- console.log(string);
- }
- if (command == "Switch") {
- let oldStr = line.shift();
- let newStr = line.shift();
- if (string.includes(oldStr)) {
- string = string.split(oldStr).join(newStr);
- }
- console.log(string);
- }
- }
- console.log(`Ready for world tour! Planned stops: ${string}`);
- }
- worldTour([
- "Hawai::Cyprys-Greece",
- "Add Stop:7:Rome",
- "Remove Stop:11:16",
- "Switch:Hawai:Bulgaria",
- "Travel",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement