使用 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
以上就是使用 JavaScript 基于算法加密字符串的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。


