Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';我不明白为什么我的ROT13转换器使用小写字母,但它不';不能用大写字母_Javascript_Regex_Function_If Statement_Rot13 - Fatal编程技术网

Javascript Can';我不明白为什么我的ROT13转换器使用小写字母,但它不';不能用大写字母

Javascript Can';我不明白为什么我的ROT13转换器使用小写字母,但它不';不能用大写字母,javascript,regex,function,if-statement,rot13,Javascript,Regex,Function,If Statement,Rot13,我不明白为什么我的ROT13转换器不能使用大写字母。 它使用小写字母。 我一直在努力寻找这个问题,但是运气不好。。 谢谢你的帮助 这是密码 var rot13 = str => { let alphabet = 'abcdefghijklmnopqrstuvwxyz'; let alphabetUp = alphabet.toUpperCase(); let ci = []; for (let i = 0; i < str.length; i++) {

我不明白为什么我的ROT13转换器不能使用大写字母。 它使用小写字母。 我一直在努力寻找这个问题,但是运气不好。。 谢谢你的帮助

这是密码


var rot13 = str => {

  let alphabet = 'abcdefghijklmnopqrstuvwxyz';
  let alphabetUp = alphabet.toUpperCase();
  let ci = [];

  for (let i = 0; i < str.length; i++) {

    let index = alphabet.indexOf(str[i]);


    // for symbols
    if (!str[i].match(/[a-z]/ig)) {

      ci.push(str[i])
      // for letters A to M
    } else if (str[i].match(/[A-M]/ig)) {
      //lowercase
      if (str[i].match(/[a-m]/g)) {
        ci.push(alphabet[index + 13])
        //uppercase (doensn't work)       
      } else if (str[i].match(/[A-M]/g)) {
        ci.push(alphabetUp[index + 13])
      }
      // for letters N to Z
    } else if (str[i].match(/[n-z]/ig)) {
      //lowercase
      if (str[i].match(/[n-z]/g)) {
        ci.push(alphabet[index - 13])
        //uppercase (doensn't work)       
      } else if (str[i].match(/[N-Z]/g)) {
        ci.push(alphabetUp[index - 13])
      }
    }

  }

  return ci.join("");
}

var rot13=str=>{
让字母表='abcdefghijklmnopqrstuvwxyz';
设alphabetUp=alphabet.toUpperCase();
设ci=[];
for(设i=0;i
通过在索引中添加13并使用模26获得新索引,然后检查原始字母是否为大写,您可以轻松完成此操作。试试这个

const rot13=str=>{
让字母表='abcdefghijklmnopqrstuvwxyz';
让newstr=[…str].map(字母=>{
设index=alphabet.indexOf(letter.toLowerCase());
如果(索引==-1){
回信;
}
指数=(指数+13)%26;
返回字母===字母.toUpperCase()?字母表[索引]。toUpperCase():字母表[索引];
})
return newstr.join(“”);
}

console.log(rot13('hello'))
让index=alphabet.indexOf(str[i])
--alphabet只包含小写字母。什么时候大写字母会给你除了-1以外的任何东西?使用
alphabet.indexOf(str[i].toLowerCase())
在测试字母是大写还是小写时,你不应该使用
i
修饰符。在测试字符串是否匹配regexp时,你也不需要
g
修饰符。你不需要正则表达式,您应该只在所需字符串上循环,使临时变量变为小写,移动它,然后使用
character===character.toUpperCase()
检查原始字符是否为大写,这应该很容易做到,这是一种比我创建的更简单的解决方案。我没有想到使用“%26”来获取索引值。