
我们需要编写一个 JavaScript 函数,该函数接收字符串并根据以下算法对其进行加密 -
字符串仅包含空格分隔的单词。
我们需要使用以下规则加密字符串中的每个单词 -
第一个字母需要转换为 ASCII 码。
立即学习“Java免费学习笔记(深入)”;
第二个字母需要与最后一个字母交换。
因此,根据此,字符串“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 基于算法加密字符串的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号