Decrypt Source Message from Code Using Algorithm in JavaScript



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
Updated on: 2021-04-19T08:18:39+05:30

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements
close