基于算法加密字符串 JavaScript
问题
我们要求编写一个 JavaScript 函数,该函数接收一个字符串,并基于以下算法对其进行加密 −
此字符串只包含以空格分隔的单词。
我们需要使用以下规则加密字符串中的每个单词 −
第一个字母需要转换为 ASCII 码。
第二个字母需要与最后一个字母对调。
因此,根据这一规则,字符串“good“将被加密成“103doo”。
示例
以下是代码 −
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));输出
103doo
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP