Liliana797979

viarno reshenie world tour - final exam

Aug 7th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function world(input) {
  2.     let tour = input.shift();
  3.    // console.log(tour);
  4.    let line = input.shift();
  5.    while (line != "Travel") {
  6.        let [command, firstArgument, secondArgument] = line.split(":");
  7.        switch (command) {
  8.            case "Add Stop":
  9.                let index = Number(firstArgument);
  10.                let str = secondArgument;
  11.                if (index < tour.length && index >= 0) {
  12.                     let split1 = tour.slice(0, index).concat(str);
  13.                     let split2 = tour.slice(index, tour.length);
  14.                     tour = split1 + split2;
  15.                }
  16.                console.log(tour);
  17.                break;
  18.             case "Remove Stop":
  19.                 let startIndex = Number(firstArgument);
  20.                 let endIndex = Number(secondArgument);
  21.                 if (startIndex < tour.length && startIndex >= 0
  22.                     && endIndex < tour.length && endIndex >= 0) {
  23.                     let split = tour.substring(startIndex, endIndex + 1);
  24.                     tour = tour.replace(split, "");
  25.                 }
  26.                 console.log(tour);
  27.                 break;
  28.            default:
  29.                if (tour.includes(firstArgument)) {
  30.                    tour = tour.split(firstArgument).join(secondArgument);
  31.                }
  32.                console.log(tour);
  33.                break;
  34.        }
  35.        line = input.shift();
  36.    }
  37.    console.log(`Ready for world tour! Planned stops: ${tour}`);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment