krasizorbov

Secret Chat

Jul 8th, 2020
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(input = []) {
  2.   let message = input.shift();
  3.   for (let i = 0; i < input.length; i++) {
  4.     let line = input[i].split(":|:");
  5.     if (line[0] === "InsertSpace") {
  6.       let index = +line[1];
  7.       let firstPart = message.substring(0, index);
  8.       let secondPart = message.substring(index);
  9.       message = firstPart + " " + secondPart;
  10.       console.log(message);
  11.     } else if (line[0] === "Reverse") {
  12.       let sub = line[1];
  13.       if (message.includes(sub)) {
  14.         let index = message.indexOf(sub);
  15.         let secondPart = message
  16.           .substring(index, index + sub.length)
  17.           .split("")
  18.           .reverse()
  19.           .join("");
  20.         let firstPart = message.substring(0, index);
  21.         let thirdPart = message.substring(index + sub.length);
  22.         message = firstPart + thirdPart + secondPart;
  23.         console.log(message);
  24.       } else {
  25.         console.log("error");
  26.       }
  27.     } else if (line[0] === "ChangeAll") {
  28.       let sub = line[1];
  29.       let str = line[2];
  30.       while (message.includes(sub)) {
  31.         let index = message.indexOf(sub);
  32.         let part = message.substring(index, index + sub.length);
  33.         message = message.replace(part, str);
  34.       }
  35.       console.log(message);
  36.     } else if (line[0] === "Reveal") {
  37.       console.log(`You have a new text message: ${message}`);
  38.       break;
  39.     }
  40.   }
  41. }
Add Comment
Please, Sign In to add comment