我正在尝试编写一个程序来解决javascript中的以下问题(写在本段下面)。我不知道为什么我的代码不起作用。有人可以帮助我吗?我是 JavaScript 新手;这是一个免费的代码训练营问题。
“现代常见的用法是 ROT13 密码,其中字母的值移动 13 位。因此,'A' ↔ 'N'、'B' ↔ 'O' 等等。
编写一个函数,将 ROT13 编码字符串作为输入并返回解码后的字符串。”
function rot13(str) { // LBH QVQ VG!
var string = "";
for(var i = 0; i < str.length; i++) {
var temp = str.charAt(i);
if(temp !== " " || temp!== "!" || temp!== "?") {
string += String.fromCharCode(13 + String.prototype.charCodeAt(temp));
} else {
string += temp;
}
}
return string;
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC")); //should decode to "FREE CODE CAMP"
使用模运算符;使句子大写;
function cipherRot13(str) {
str = str.toUpperCase();
return str.replace(/[A-Z]/g, rot13);
function rot13(correspondance) {
const charCode = correspondance.charCodeAt();
//A = 65, Z = 90
return String.fromCharCode(
((charCode + 13) <= 90) ? charCode + 13
: (charCode + 13) % 90 + 64
);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)