Decrypting source message from a code based on some algorithm in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a decrypted message and returns its source message.

All we know is the algorithm used to encrypt that message.

And the algorithm is −

  • Reverse the message string.
  • Replace every letter with its ASCII code in quotes (A to '65', h to '104' and so on).
  • Insert digits and spaces as is.

Example

Following is the code −

 Live Demo

const str = '12 hello world 30';
const decryptString = (str = '') => {
   const alpha = 'abcdefghijklmnopqrstuvwxyz';
   let res = '';
   for(let i = str.length - 1; i >= 0; i--){
      const el = str[i];
      if(alpha.includes(el.toLowerCase())){
         res += `'${el.charCodeAt(0)}'`;
      }else{
         res += el;
      };
   };
   return res;
};
console.log(decryptString(str));

Output

Following is the console output −

03 '100''108''114''111''119' '111''108''108''101''104' 21
raja
Published on 19-Apr-2021 08:18:05
Advertisements