如何使用 String.charCodeAt();一次获取和存储字符串中每个字母的字符代码值并存储它们?

How to use String.charCodeAt(); to get and store char code values of each letter of a string at once and store them?

这是我的代码,用于获取字符串第一个字母的字符代码值,然后将该字符代码值转换为负 13(我需要在我的问题中这样做)。然后我转换该字符代码以获得 character.As 到目前为止,我能够为给定输入中的第一个或任何字符执行此操作 string.But 我想要做的是获取每个字母的字符代码一次全部输入字符串,然后将每个字符代码递减 13,然后将每个字符代码转换回字母并输出整个转换后的 string.And 而且我的输入不固定,有许多测试用例保持 changing.Please 帮助我解决它,将这些点保留在 mind.Here 我的代码中:

function rot13(str) { // LBH QVQ VG! It is just a useless comment.

var a=str.charCodeAt(0);//I am able to  get char code of 0 or any other  index but just one at a tim,how to do it for all the index values?
a-=13;//I need to decrement eaach char code by 13

var b=String.fromCharCode(a);//and from each char code I need to give back aletter but I want to return a whole converted string back as ouput not just a single converted letter.How to do it?
return b;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");

您需要遍历字符串的所有字符,然后继续将更改后的值附加到字符串 b,如下所示:

function rot13(s)
 {
    return (s ? s : this).split('').map(function(_)
     {
        if (!_.match(/[A-Za-z]/)) return _;
        c = Math.floor(_.charCodeAt(0) / 97);
        k = (_.toLowerCase().charCodeAt(0) - 83) % 26 || 26;
        return String.fromCharCode(k + ((c == 0) ? 64 : 96));
     }).join('');
 }

alert(rot13("SERR PBQR PNZC"));