这样写是通过了,但是我不确定题目本意是否想要的是这种写法。。。
seakingxc opened this issue · comments
Challenge Caesars Cipher has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function rot13(str) { // LBH QVQ VG!
var result="";
for(var i=0;i<str.length;i++){
//匹配到非字母的元素直接跳过
if(str.charAt(i).match(/[A-Z]/g)){
//先把字母转化为unicode编码,这样就变成了数字
//每个数字-13和+13即移位13个位置
var charCode = str.charCodeAt(i);
if(charCode-13<65){
charCode +=13;
}else{
charCode-=13;
}
//把移位后的数字unicode码再转化为对应的字母
result+=String.fromCharCode(charCode);
}else{
result+=str.charAt(i);
}
}
return result;
}
rot13("SERR PBQR PNZC"); // 你可以修改这一行来测试你的代码
@seakingxc 这样写当然可以。。只是
- 既然只是检测,那其实不需要
match
,可以直接通过charCode
的数字范围来判断。 - 如果用正则,那不如直接用
replace
方法。