问题
我们需要编写一个 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
登录后复制
以上就是使用 JavaScript 基于算法加密字符串的详细内容,更多请关注www.92cms.cn其它相关文章!






