vladovip

DecryptCommands_JS FUND

Apr 9th, 2022 (edited)
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function decryptingCommands(inputArr) {
  2.   let text = inputArr.shift();
  3.   let commandline = inputArr.shift();
  4.  
  5.   while (commandline != "Finish") {
  6.     let tokens = commandline.split(" ");
  7.     let command = tokens[0];
  8.  
  9.     if (command == "Replace") {
  10.       let currentChar = tokens[1];
  11.       let replacementChar = tokens[2];
  12.       textArr = text.split("");
  13.       for (let index = 0; index < textArr.length; index++) {
  14.         if (currentChar == textArr[index]) {
  15.           textArr[index] = replacementChar;
  16.         }
  17.       }
  18.       text = textArr.join("");
  19.       console.log(text);
  20.     }
  21.     if (command == "Cut") {
  22.       let startIndex = Number(tokens[1]); // StarIndex 1; EndIndex 4;  text ->  WELIKESOFTUNI
  23.       let endIndex = Number(tokens[2]);
  24.  
  25.       if (startIndex >= 0 && startIndex < text.length &&  endIndex >= 0 &&  endIndex < text.length) {
  26.         let leftPart = text.slice(0, startIndex+1);
  27.         let rightPart = text.slice(endIndex+2, text.length);
  28.         text = leftPart + rightPart;
  29.         console.log(text);
  30.       } else {
  31.         console.log(`Invalid indices!`);
  32.       }
  33.     }
  34.  
  35.     if (tokens[0] + " " + tokens[1] == "Make Upper") {
  36.       text = text.toUpperCase();
  37.       console.log(text);
  38.     }
  39.     if (tokens[0] + " " + tokens[1] == "Make Lower") {
  40.       text = text.toLowerCase();
  41.       console.log(text);
  42.     }
  43.  
  44.     if (command == "Check") {
  45.       let subStrTxt = tokens[1];
  46.       if (text.includes(subStrTxt) == true) {
  47.         console.log(`Message contains ${subStrTxt}`);
  48.       } else {
  49.         console.log(`Message doesn't contain ${subStrTxt}`);
  50.      }
  51.    }
  52.  
  53.    if (command == "Sum") {
  54.      let startIndex = Number(tokens[1]);
  55.      let endIndex = Number(tokens[2]);
  56.      
  57.      if ( startIndex >= 0 && startIndex < text.length &&  endIndex >= 0 &&  endIndex < text.length ) {
  58.        let subStrText = text.substring(startIndex, endIndex + 1);
  59.        let tempArrsubStrText = subStrText.split("");
  60.        let sumASCIIChar = 0;
  61.        for (let charOfSubstr of tempArrsubStrText) {
  62.          sumASCIIChar += charOfSubstr.charCodeAt();
  63.        }
  64.        console.log(sumASCIIChar);
  65.      } else {
  66.        console.log(`Invalid indices!`);
  67.      }
  68.    }
  69.  
  70.    commandline = inputArr.shift();
  71.  }
  72. }
  73.  
  74. decryptingCommands([
  75.  "ILikeSoftUni",
  76.  "Replace I We",
  77.  "Make Upper",
  78.  "Check SoftUni",
  79.  "Sum 1 4",
  80.  "Cut 1 4",
  81.  "Finish",
  82. ]);
  83.  
  84.  
  85. console.log(`----------`);
  86.  
  87. decryptingCommands([
  88.    "HappyNameDay",
  89.    "Replace p r",
  90.    "Make Lower",
  91.    "Cut 2 23",
  92.    "Sum -2 2",
  93.    "Finish", ]);
  94.  
Add Comment
Please, Sign In to add comment