Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- secretChat = (input = []) => {
- let message = input.shift();
- while((cmd = input.shift().split(':|:'))[0] !== 'Reveal') {
- if(cmd[0] === 'InsertSpace') {
- const index = Number(cmd[1]);
- message = message.substring(0, index) + ' ' + message.substring(index);
- console.log(message);
- } else if(cmd[0] === 'Reverse') {
- const substring = cmd[1];
- const startIndex = message.indexOf(substring);
- if(startIndex !== -1) {
- const endIndex = startIndex + substring.length;
- // message.split(substring).join('') + substring.split('').reverse().join('')
- message = message.substring(0, startIndex) + message.substring(endIndex) + substring.split('').reverse().join('');
- console.log(message);
- } else {
- console.log('error');
- }
- } else { // 'ChangeAll'
- const [ substring, replacement ] = cmd.slice(1);
- message = message.replace(new RegExp(substring, 'g'), replacement);
- console.log(message);
- }
- }
- console.log(`You have a new text message: ${message}`);
- };
Add Comment
Please, Sign In to add comment