Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //01. World Tour
- function solve(input) {
- let tour = input.shift();
- let line = input.shift();
- while (line !== 'Travel') {
- let [command, ...rest] = line.split(':');
- command = command.trim()
- if (command === "Add Stop") {
- let index = Number(rest[0]);
- let string = rest[1];
- if (index >= 0 && index <= tour.length) {
- tour = tour.split('');
- tour.splice(index, 0, string);
- tour = tour.join('');
- }
- } else if (command === "Remove Stop") {
- let strIndex = Number(rest[0]);
- let endIndex = Number(rest[1]);
- if (strIndex >= 0 && strIndex < tour.length &&
- endIndex >= 0 && endIndex < tour.length) {
- tour = tour.split('');
- tour.splice(strIndex, endIndex - strIndex + 1);
- tour = tour.join('');
- }
- } else if (command === "Switch") {
- let oldString = rest[0];
- let newString = rest[1];
- let rgx = new RegExp(oldString, 'g');
- tour = tour.replace(rgx, newString);
- }
- console.log(tour);
- line = input.shift();
- }
- console.log(`Ready for world tour! Planned stops: ${tour}`)
- }
- solve(['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