AreWe

Secret Chat

Jun 29th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. secretChat = (input = []) => {
  2.     let message = input.shift();
  3.     while((cmd = input.shift().split(':|:'))[0] !== 'Reveal') {
  4.         if(cmd[0] === 'InsertSpace') {
  5.             const index = Number(cmd[1]);
  6.             message = message.substring(0, index) + ' ' + message.substring(index);
  7.             console.log(message);
  8.         } else if(cmd[0] === 'Reverse') {
  9.             const substring = cmd[1];
  10.             const startIndex = message.indexOf(substring);
  11.             if(startIndex !== -1) {
  12.                 const endIndex = startIndex + substring.length;
  13.                 // message.split(substring).join('') + substring.split('').reverse().join('')
  14.                 message = message.substring(0, startIndex) + message.substring(endIndex) + substring.split('').reverse().join('');
  15.                 console.log(message);
  16.             } else {
  17.                 console.log('error');
  18.             }
  19.         } else { // 'ChangeAll'
  20.             const [ substring, replacement ] = cmd.slice(1);
  21.             message = message.replace(new RegExp(substring, 'g'), replacement);
  22.             console.log(message);
  23.         }
  24.     }
  25.  
  26.     console.log(`You have a new text message: ${message}`);
  27. };
Add Comment
Please, Sign In to add comment