Advertisement
teofarov13

Untitled

Apr 1st, 2023
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function secretChat(data) {
  2.     let msg = data.shift();
  3.  
  4.     let command = data.shift();
  5.  
  6.     while (command !== "Reveal") {
  7.         command = command.split(":|:")
  8.         switch (command[0]) {
  9.             case "ChangeAll":
  10.                 let forReplace = command[1];
  11.                 let replaced = command[2];
  12.                 for (let char of msg) {
  13.                     if (char == forReplace) {
  14.                         msg = msg.replace(forReplace, replaced)
  15.                     }
  16.                 }
  17.                 console.log(msg);
  18.                 break;
  19.             case "Reverse":
  20.                 let reversed=command[1]
  21.                 if (msg.includes(reversed)) {
  22.                 msg = msg.replace(reversed,'');
  23.                     let forReverse = reversed.split("").reverse().join("")
  24.                     msg+=forReverse
  25.  
  26.                     console.log(msg);    
  27.                 } else {
  28.                     console.log('error');
  29.                 }
  30.                 break;
  31.             case "InsertSpace":
  32.                 let index =command[1]
  33.               //  let empty = " "
  34.                 let leftSide = msg.substring(0, index)
  35.                 let rightSide = msg.substring(index);
  36.                 msg = leftSide + " " + rightSide;
  37.                 console.log(msg);
  38.                 break;
  39.         }
  40.         command = data.shift();
  41.     }
  42.     if (command == "Reveal") {
  43.         console.log(`You have a new text message: ${msg}`);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement