Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function worldTour(input){
- let stops = input.shift();
- let command = input.shift();
- while(command !== 'Travel') {
- let [action, ...data] = command.split(':');
- if(action.includes('Add')) {
- let [index, string] = data;
- index = Number(index);
- if(index >= 0 && index < stops.length) {
- stops = stops.slice(0, index) + string + stops.slice(index);
- }
- console.log(stops);
- } else if (action.includes('Remove')) {
- let [startIndex, endIndex] = data.map(Number);
- if(startIndex >= 0 && endIndex >= startIndex && endIndex < stops.length) {
- let substring = stops.slice(startIndex, endIndex+1);
- stops = stops.replace(substring, '');
- }
- console.log(stops);
- } else {
- let [oldString, newString] = data;
- if(stops.includes(oldString)) {
- let splitted = stops.split(oldString);
- stops = splitted.join(newString);
- }
- console.log(stops);
- }
- command = input.shift();
- }
- console.log(`Ready for world tour! Planned stops: ${stops}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment