Advertisement
TZinovieva

The Imitation Game JS Fundamentals

Mar 25th, 2023
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theImitationGame(arr) {
  2.     let message = arr.shift().split(',').toString();
  3.  
  4.     for (let line of arr) {
  5.         let tokens = line.split('|');
  6.         let command = tokens[0];
  7.        
  8.         if (command === "Move") {
  9.             let part = message.slice(tokens[1]);
  10.             let subs = message.slice(0, tokens[1]);
  11.             message = part + subs;
  12.         } else if (command === "Insert") {
  13.             message = message.substring(0, tokens[1]) + tokens[2] + message.substring(tokens[1]);
  14.         } else if (command === "ChangeAll") {
  15.             for (let letter of message) {
  16.                 if (letter === tokens[1]) {
  17.                     message = message.replace(letter, tokens[2]);
  18.                 }
  19.             }
  20.         } else if (command === 'Decode') {
  21.             console.log(`The decrypted message is: ${message}`);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement