Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Encrypting a string based on an algorithm in JavaScript
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'.
Algorithm Breakdown
Let's understand how the encryption works with the word 'good':
- First letter 'g' ? ASCII code 103
- Second letter 'o' switches with last letter 'd'
- Middle letters 'o' remain in position
- Result: '103' + 'd' + 'o' + 'o' = '103doo'
Single Word Encryption
const str = 'good';
const encryptString = (str = '') => {
const [first, second] = str.split('');
const last = str[str.length - 1];
let res = '';
// Convert first letter to ASCII
res += first.charCodeAt(0);
// Add last letter
res += last;
// Add middle letters (if any)
for(let i = 2; i < str.length - 1; i++){
const el = str[i];
res += el;
}
// Add second letter at the end
res += second;
return res;
};
console.log(encryptString(str));
103doo
Multiple Words Encryption
To handle multiple space-separated words, we need to split the string and encrypt each word individually:
const encryptSentence = (sentence = '') => {
const words = sentence.split(' ');
const encryptedWords = words.map(word => {
if (word.length <= 1) return word;
const [first, second] = word.split('');
const last = word[word.length - 1];
let res = '';
res += first.charCodeAt(0);
res += last;
for(let i = 2; i < word.length - 1; i++){
res += word[i];
}
res += second;
return res;
});
return encryptedWords.join(' ');
};
console.log(encryptSentence('good morning world'));
console.log(encryptSentence('hello javascript'));
103doo 109ornin 119orlo 104ello 106avascrip
Edge Cases Handling
The algorithm should handle edge cases like single-character words and empty strings:
const encryptStringSafe = (str = '') => {
if (str.length <= 1) return str;
if (str.length === 2) {
return str.charCodeAt(0) + str[1];
}
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++){
res += str[i];
}
res += second;
return res;
};
console.log(encryptStringSafe('a')); // Single character
console.log(encryptStringSafe('hi')); // Two characters
console.log(encryptStringSafe('code')); // Normal case
a 104i 99odc
Conclusion
This encryption algorithm transforms strings by converting the first character to ASCII and swapping the second and last characters. It's a simple substitution cipher suitable for basic text obfuscation in JavaScript applications.
Advertisements
