加密为Unicode的字母不起作用
这是从freecodecamp的项目,该项目通过字母的值加密的字符串被13个位置移动 所有字母将是大写。请勿改变任何非字母特征(即空间,标点符号),而要传递。 我测试了REGEX NUMADD的NUMADD等于NUM+13或NUMADD-26。一些信件出错,不要退还大写字母 请有人解释怎么了
function rot13(str) {
var str2 = '';
var arr1 = []
var arr2 = []
var reg = /[^A-Z]/
// var char
if (str.includes(' ') == false) {
for (let i = 0; i < str.length; i++) {
var char = '';
var numres
let num = str.charCodeAt(i)
// console.log(num)
// var numadd = num + 13;
// if num+13 not uppercase
if (reg.test(num + 13)) {
numres = num - 13
}
// if num+13 is uppercase
else {
numres = num + 13
}
char = String.fromCharCode(numres)
str2 += char
}
console.log(str2);
}
}
rot13("SERRPBQRPNZC");
This is project from freecodecamp where a string is encrypt by the values of the letters are shifted by 13 places
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
i test numadd by regex numadd is either equal to num+13 or numadd-26. some letter went wrong and do not return an uppercase letter
please someone explain whats wrong
function rot13(str) {
var str2 = '';
var arr1 = []
var arr2 = []
var reg = /[^A-Z]/
// var char
if (str.includes(' ') == false) {
for (let i = 0; i < str.length; i++) {
var char = '';
var numres
let num = str.charCodeAt(i)
// console.log(num)
// var numadd = num + 13;
// if num+13 not uppercase
if (reg.test(num + 13)) {
numres = num - 13
}
// if num+13 is uppercase
else {
numres = num + 13
}
char = String.fromCharCode(numres)
str2 += char
}
console.log(str2);
}
}
rot13("SERRPBQRPNZC");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
reg.test(num + 13)
检查编号的文本表示num + 13
(应用 nimit 键入转换)总是说 true (不匹配大写字母)和始终是计算numres = numres = num -13
。使用
reg.test(String.fromcharcode(num + 13))
,如下:reg.test(num + 13)
checks the textual representation of the numbernum + 13
(applying implicit type conversion) so it always says True (do not match an uppercase letter) and always is computednumres = num - 13
.Use
reg.test(String.fromCharCode(num + 13))
instead as follows: