t_sh0w

01. Secret Chat

Apr 10th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.   let text = input.shift();
  3.   input.pop();
  4.  
  5.   for (const currentLine of input) {
  6.     let [command, tokenOne, tokenTwo] = currentLine.split(":|:");
  7.  
  8.     switch (command) {
  9.       case "InsertSpace":
  10.         tokenOne = Number(tokenOne);
  11.         text = text.split("");
  12.         text.splice(tokenOne, 0, " ");
  13.         text = text.join("");
  14.         console.log(text);
  15.         break;
  16.  
  17.       case "Reverse":
  18.         if (text.includes(tokenOne)) {
  19.           let startIndex = text.indexOf(tokenOne);
  20.           let part = text.substr(startIndex, tokenOne.length);
  21.           let reversedPart = part.split("").reverse().join("");
  22.           text = text.replace(part, "");
  23.           text += reversedPart;
  24.           console.log(text);
  25.         } else {
  26.           console.log("error");
  27.         }
  28.         break;
  29.  
  30.       case "ChangeAll":
  31.         while (text.includes(tokenOne)) {
  32.           text = text.replace(tokenOne, tokenTwo);
  33.         }
  34.         console.log(text);
  35.         break;
  36.     }
  37.   }
  38.   console.log(`You have a new text message: ${text}`);
  39. }
Add Comment
Please, Sign In to add comment