首页 > Caesars Cipher凯撒密码为何没有返回加密后的字符串?

Caesars Cipher凯撒密码为何没有返回加密后的字符串?

freecodecamp的题目.https://freecodecamp.cn/chall...

官方wiki里用的map等方法,实在是理解不了,自己用了两个for循环做的,代码如下:

function rot13(str) { 
  var strup=str.toUpperCase(); //把所有字母都转成大写
  var charcodearr=[];
  var rotcodearr=[];
  for (var i = 0; i < strup.length; i++) { //获取strup每个字母的code并push进数组charcodearr里
      var strcode=(strup.charCodeAt(i));
      charcodearr.push(strcode);
  }
 for (var j = 0; j < charcodearr.length; j++) { //凯撒加密,加密后的值push进数组rotcodearr里
    if (charcodearr[j]<65) {rotcodearr.push(charcodearr[j]);}
        else if (charcodearr[j]<78) {rotcodearr.push(charcodearr[j]+13);}
        else if (charcodearr[j]<91) {rotcodearr.push(charcodearr[j]-13);}
        else if(charcodearr[j]>91) {rotcodearr.push(charcodearr[j]);}
}


  var encodestr=rotcodearr.toString(); 
  return String.fromCharCode(encodestr);//加密后的数组转换成string并返回成字母
}

// Change the inputs below to test
rot13("Abc XYz");

最后没过关,返回的是个空字符串,百思不得其解.


最后一步传参不对,

var encodestr=rotcodearr.toString(); 
  return String.fromCharCode(encodestr);

改成

return String.fromCharCode.apply(this,rotcodearr)
【热门文章】
【热门文章】