Advertisement
bebo231312312321

Untitled

Mar 25th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theImitationGame(input) {
  2.     let secret = input.shift()
  3.     let newArr = input.splice(0, input.indexOf("Decode"))
  4.         .forEach(elem => {
  5.             let [command, ...args] = elem.split("|")
  6.  
  7.             switch (command) {
  8.                 case "Move": {
  9.                     let num = Number(args[0])
  10.                     let firstPart = secret.substring(0, num)
  11.                     let secondPart = secret.substring(num)
  12.                     secret = secondPart + firstPart
  13.                 } break;
  14.                 case "Insert": {
  15.                     let [index, token] = args
  16.                     index = +index
  17.                     let first = secret.substring(0, index)
  18.                     let second = secret.substring(index)
  19.                     secret = first + token + second
  20.                 } break;
  21.                 case "ChangeAll": {
  22.                     let [substrings, replaced] = args
  23.                     substrings = substrings.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&")
  24.                     let regex = new RegExp(substrings, "g")
  25.                     secret = secret.replace(regex, replaced)
  26.                     // друг вариант
  27.                     //secret = secret.split(substrings).join(replaced)
  28.                 } break;
  29.             }
  30.         })
  31.     console.log(`The decrypted message is: ${secret}`)
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement