Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []) {
- let text = input.shift();
- input.pop();
- for (const currentLine of input) {
- let [command, tokenOne, tokenTwo] = currentLine.split(":|:");
- switch (command) {
- case "InsertSpace":
- tokenOne = Number(tokenOne);
- text = text.split("");
- text.splice(tokenOne, 0, " ");
- text = text.join("");
- console.log(text);
- break;
- case "Reverse":
- if (text.includes(tokenOne)) {
- let startIndex = text.indexOf(tokenOne);
- let part = text.substr(startIndex, tokenOne.length);
- let reversedPart = part.split("").reverse().join("");
- text = text.replace(part, "");
- text += reversedPart;
- console.log(text);
- } else {
- console.log("error");
- }
- break;
- case "ChangeAll":
- while (text.includes(tokenOne)) {
- text = text.replace(tokenOne, tokenTwo);
- }
- console.log(text);
- break;
- }
- }
- console.log(`You have a new text message: ${text}`);
- }
Add Comment
Please, Sign In to add comment