

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encrypting a string based on an algorithm in JavaScript
Problem
We are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm −
- The string contains only space separated words.
- We need to encrypt each word in the string using the following rules −
- The first letter needs to be converted to its ASCII code.
- The second letter needs to be switched with the last letter.
Therefore, according to this, the string ‘good’ will be encrypted as ‘103doo’.
Example
Following is the code −
const str = 'good'; const encyptString = (str = '') => { const [first, second] = str.split(''); const last = str[str.length - 1]; let res = ''; res += first.charCodeAt(0); res += last; for(let i = 2; i < str.length - 1; i++){ const el = str[i]; res += el; }; res += second; return res; }; console.log(encyptString(str));
Output
Following is the console output −
103doo
- Related Questions & Answers
- Encrypting a string based on an algorithm using JavaScript
- Encrypting a string JavaScript
- Shuffling string based on an array in JavaScript
- Encrypting a string using Caesar Cipher in JavaScript
- Shifting string letters based on an array in JavaScript
- Change string based on a condition - JavaScript
- Returning acronym based on a string in JavaScript
- Decrypting source message from a code based on some algorithm in JavaScript
- Encoding string based on character frequency in JavaScript
- Forming and matching strings of an array based on a random string in JavaScript
- Filter an object based on an array JavaScript
- Encrypting censored words using JavaScript
- Modify an array based on another array JavaScript
- Create an object based on 2 others in JavaScript
- Constructing a string based on character matrix and number array in JavaScript
Advertisements