- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decrypting source message from a code based on some 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 −
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
Advertisements