Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let message = input.shift();
- const commands = (command, firstArgument, secondArgument) => {
- let hasError = false;
- switch (command) {
- case 'InsertSpace':
- message = `${message.substring(0, Number(firstArgument))} ${message.substring(Number(firstArgument))}`;
- break;
- case 'Reverse':
- if (message.includes(firstArgument)) {
- message = message.replace(firstArgument, '');
- let reversedSubstring = '';
- for (const char of firstArgument) {
- reversedSubstring = char + reversedSubstring;
- }
- message += reversedSubstring;
- } else {
- console.log('error');
- hasError = true;
- }
- break;
- case 'ChangeAll':
- while (message.includes(firstArgument)) {
- message = message.replace(firstArgument, secondArgument);
- }
- break;
- default:
- }
- if (!hasError) {
- console.log(message);
- }
- };
- let line = input.shift();
- while (line !== 'Reveal') {
- const [command, firstArgument, secondArgument] = line.split(':|:');
- commands(command, firstArgument, secondArgument);
- line = input.shift();
- }
- console.log(`You have a new text message: ${message}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement