Todorov_Stanimir

02. Deciphering Demo Technology Fundamental Exam. 06.04.2019

May 17th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deciphering(input) {
  2.  
  3.     let encryptedString = input.shift();
  4.     let output = '';
  5.     let isNotWrongBook = true;
  6.     let result = '';
  7.     for (let i = 0; i < encryptedString.length; i++) {
  8.         let charIndex = encryptedString.charCodeAt((i));
  9.         if ((100 <= charIndex && charIndex <= 122) ||
  10.             charIndex === 123 ||
  11.             charIndex === 124 ||
  12.             charIndex === 125 ||
  13.             charIndex === 35) {
  14.  
  15.             charIndex = charIndex - 3;
  16.             output += String.fromCharCode(charIndex);
  17.         } else {
  18.             console.log('This is not the book you are looking for.');
  19.             isNotWrongBook = false;
  20.             break;
  21.         }
  22.     }
  23.  
  24.     if (isNotWrongBook) {
  25.         let [substr1, substr2] = input.shift().split(' ');
  26.  
  27.         for (let i = 0; i < output.length; i += substr1.length) {
  28.             let curstr = output.substr(i, substr1.length);
  29.             if (curstr === substr1) {
  30.                 curstr = substr2;
  31.             }
  32.             result += curstr;
  33.         }
  34.     }
  35.     console.log(result);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment