Liliana797979

imitation game - final exam - fundamentals

Aug 6th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function imitationGame(input) {
  4.   let textToDecode = input.shift();
  5.  
  6.   let actions = {
  7.     'ChangeAll': changeall,
  8.     'Move': move,
  9.     'Insert': insertletter,
  10.   };
  11.  
  12.   while (input[0] !== "Decode") {
  13.     let line = input.shift();
  14.     let [command, param1, param2] = line.split("|");
  15.     let action = actions[command];
  16.     action(param1, param2);
  17.  
  18.   }
  19.   console.log(`The decrypted message is: ${textToDecode}`);
  20.  
  21.   function changeall(param1, param2) {
  22.     while(textToDecode.includes(param1)){
  23.     textToDecode = textToDecode.replace(param1, param2);
  24.   }
  25. }
  26.   function move(n,param2){
  27.     n = Number(n);
  28.     let left= textToDecode.substring(0,n);
  29.     let right = textToDecode.substring(n);
  30.     textToDecode = right+ left;
  31.   }
  32.   function insertletter(param1,param2){
  33.       param1 = Number(param1);
  34.  
  35.       let left = textToDecode.substring(0,param1);
  36.       let right = textToDecode.substring(param1);
  37.       textToDecode = left + param2 + right;
  38.  
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment