Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function world(input) {
- let tour = input.shift();
- // console.log(tour);
- let line = input.shift();
- while (line != "Travel") {
- let [command, firstArgument, secondArgument] = line.split(":");
- switch (command) {
- case "Add Stop":
- let index = Number(firstArgument);
- let str = secondArgument;
- if (index < tour.length && index >= 0) {
- let split1 = tour.slice(0, index).concat(str);
- let split2 = tour.slice(index, tour.length);
- tour = split1 + split2;
- }
- console.log(tour);
- break;
- case "Remove Stop":
- let startIndex = Number(firstArgument);
- let endIndex = Number(secondArgument);
- if (startIndex < tour.length && startIndex >= 0
- && endIndex < tour.length && endIndex >= 0) {
- let split = tour.substring(startIndex, endIndex + 1);
- tour = tour.replace(split, "");
- }
- console.log(tour);
- break;
- default:
- while (tour.includes(firstArgument) === true) {
- tour = tour.replace(firstArgument, secondArgument);
- }
- console.log(tour);
- break;
- }
- line = input.shift();
- }
- console.log(`Ready for world tour! Planned stops: ${tour}`);
- }
- world(["Hawai::Cyprys-Greece",
- "Add Stop:7:Rome",
- "Remove Stop:11:16",
- "Switch:Hawai:Bulgaria",
- "Travel"]);
Advertisement
Add Comment
Please, Sign In to add comment