Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) { //100т.
- let str = inputArr.shift();
- for (const line of inputArr) {
- if (line === "Travel") {
- console.log(`Ready for world tour! Planned stops: ${str}`)
- break;
- } else {
- let tokens = line.split(":");
- if (line.includes("Add Stop")) { //Add Stop:{index}:{string} – insert the given string at that index only if the index is valid
- let idx = Number(tokens[1]);
- let newStop = tokens[2];
- // console.log(newStop)
- if (idx >= 0 && idx <= str.length - 1) { //ако е валиден индекса
- str = str.slice(0, idx) + newStop + str.slice(idx);
- //console.log(str);
- }
- console.log(str);
- } else if (line.includes("Remove Stop")) { //Remove Stop:{start_index}:{end_index} – remove the elements of the string from the starting
- let startIdx = Number(tokens[1]);
- let endIdx = Number(tokens[2]);
- if (startIdx >= 0 && startIdx <= str.length - 1 && endIdx >= 0 && endIdx <= str.length - 1) {
- let strToRemove = str.substring(startIdx, endIdx + 1);
- str = str.replace(strToRemove, "");
- }
- console.log(str)
- } else if (line.includes("Switch")) { //Switch:{old_string}:{new_string} – if the old string is in the initial string, replace it with the new one. (all occurrences)
- let oldStr = tokens[1];
- let newStr = tokens[2];
- if (str.includes(oldStr)) {
- str = str.split(oldStr).join(newStr);
- //console.log(str);
- }
- console.log(str); //или може би тук трябва да се принтне, а не горе в if-а
- }
- }
- }
- }
- solve(['Hawai::Cyprys-Greece', 'Add Stop:7:Rome', 'Remove Stop:11:16', 'Switch:Hawai:Bulgaria', 'Travel']);
Add Comment
Please, Sign In to add comment