Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let str = input.shift()
- const actions = { "Add Stop": addStop, "Remove Stop": remove, Switch: switched }
- function addStop(index, newPart) {
- if (str[index] !== undefined) { //here
- index = Number(index)
- let firstPart = str.substring(0, index);
- let secondPart = str.substring(index);
- str = firstPart + newPart + secondPart;
- }
- console.log(str)
- }
- function remove(startIndex, endIndex) {
- startIndex = Number(startIndex);
- endIndex = Number(endIndex);
- if (str[startIndex] !== undefined && str[endIndex] !== undefined) { //here
- let cut = str.substring(startIndex, endIndex + 1);
- str = str.replace(cut, "")
- }
- console.log(str)
- }
- function switched(oldItem, newItem) {
- //if (str.includes(oldItem)) {
- //
- // while (str.includes(oldItem)) {
- //
- // str = str.replace(oldItem, newItem)
- //
- // }
- //
- //}
- if (str.includes(oldItem)) { //here
- str = str.split(oldItem).join(newItem); //here
- } //here
- console.log(str)
- }
- for (let i = 0; i < input.length; i++) {
- let [command, a, b] = input[i].split(":")
- if (input[i] === "Travel") break
- actions[command](a, b)
- }
- console.log(`Ready for world tour! Planned stops: ${str}`)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement