Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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
如何处理Caesars密码中的负移位(Javascript)_Javascript_Caesar Cipher - Fatal编程技术网

如何处理Caesars密码中的负移位(Javascript)

如何处理Caesars密码中的负移位(Javascript),javascript,caesar-cipher,Javascript,Caesar Cipher,我试图通过奥丁凯撒密码,测试要求能够转换负移位。根据我目前的代码,我能够转换小写,但我有一些问题,与B或W it('works with negative shift', function() { expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!'); 然而,在返回时,我的代码吐了出来 “你好,=世界!” 太近了!我一直在试图弄清楚它是什么,但我不确定我在这里做错了什么,因为“H”在起作用 我已经多次重写了这件事,我总

我试图通过奥丁凯撒密码,测试要求能够转换负移位。根据我目前的代码,我能够转换小写,但我有一些问题,与B或W

it('works with negative shift', function() {
    expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
然而,在返回时,我的代码吐了出来

“你好,=世界!”

太近了!我一直在试图弄清楚它是什么,但我不确定我在这里做错了什么,因为“H”在起作用

我已经多次重写了这件事,我总是在这里结束。我相信这只是一个数字或什么。然而,这超出了我现在所知道或能理解的范围

提前谢谢大家,很抱歉问这么简单的问题

const caesar = function(message, shift) {
    return message 
    .split("") //splits it into an array
    .map(message => { //does the following to each element in the array
        normalStr = String.fromCharCode(message.charCodeAt())
        prePoint = message.charCodeAt() //gets the charcode of element  
    //if/else checks to see if upper or lower case
    if (prePoint >= 65 && prePoint <= 90) { //upper case
        return String.fromCharCode(((prePoint - 65 + shift) % 26) + 65);
    } else if (prePoint >= 97 && prePoint <= 122){ //lower case
        return String.fromCharCode((prePoint -97 + shift % 26) + 97) 
    }  else {
        return normalStr

        //1 func proc uppoer case
        //1 func proc lowercase
        //1 func proc non upper/lower case
    }})
    .join("")

}
const caesar=函数(消息、移位){
回信
.split(“”//将其拆分为一个数组
.map(message=>{//对数组中的每个元素执行以下操作
normalStr=String.fromCharCode(message.charCodeAt())
prePoint=message.charCodeAt()//获取元素的charcode
//if/else检查大写还是小写

如果(prePoint>=65&&prePoint=97&&prePoint您的代码只适用于正的凯撒移位,因为在
String.fromCharCode((prePoint-65+shift)%26)+65);

prePoint-65+shift
可能低于零(使用prePoint=
B
=66和shift=
-5
您将得到
-4

您可以通过检查
(prePoint-65+shift)
的结果是否为负值来解决此问题,如果为负值,则向其添加26:

let newPoint = (prePoint - 65 + shift) % 26;
if(newPoint < 0) newPoint += 26;
return String.fromCharCode(newPoint + 65);
let newPoint=(prePoint-65+shift)%26;
如果(newPoint<0)newPoint+=26;
返回字符串.fromCharCode(newPoint+65);
(小写字母相同)

或者,您可以在函数开始时将负移位转换为正移位(一个-5 caear移位与一个21 caesar移位相同):

if(shift<0){shift=26+(shift%26);}
完整示例:

功能caesar(消息、班次){
如果(移位<0){
班次=26+(班次%26);
}
回信
.split(“”//将其拆分为一个数组
.map(message=>{//对数组中的每个元素执行以下操作
normalStr=String.fromCharCode(message.charCodeAt())
prePoint=message.charCodeAt()//获取元素的charcode
//if/else检查大写还是小写

如果(prePoint>=65&&prePoint=97&&prePoint您的代码只适用于正的凯撒移位,因为在
String.fromCharCode((prePoint-65+shift)%26)+65);

prePoint-65+shift
可能低于零(使用prePoint=
B
=66和shift=
-5
您将得到
-4

您可以通过检查
(prePoint-65+shift)
的结果是否为负值来解决此问题,如果为负值,则向其添加26:

let newPoint = (prePoint - 65 + shift) % 26;
if(newPoint < 0) newPoint += 26;
return String.fromCharCode(newPoint + 65);
let newPoint=(prePoint-65+shift)%26;
如果(newPoint<0)newPoint+=26;
返回字符串.fromCharCode(newPoint+65);
(小写字母相同)

或者,您可以在函数开始时将负移位转换为正移位(一个-5 caear移位与一个21 caesar移位相同):

if(shift<0){shift=26+(shift%26);}
完整示例:

功能caesar(消息、班次){
如果(移位<0){
班次=26+(班次%26);
}
回信
.split(“”//将其拆分为一个数组
.map(message=>{//对数组中的每个元素执行以下操作
normalStr=String.fromCharCode(message.charCodeAt())
prePoint=message.charCodeAt()//获取元素的charcode
//if/else检查大写还是小写

如果(prepint>=65&&prepint=97&&prepint嘿,就是这个!我甚至没有意识到21移位和-5是一样的!这是因为字母表中有26个字符,26-5=21对吗?非常感谢你给我提供的帮助!@ChrisKing你是对的,你可以做负移位转换->正移位转换因为字母表是循环的,就像你26-5的例子:)很高兴我能帮忙;)嘿,就是这个!我甚至都没意识到21移位和-5是一样的!这是因为字母表中有26个字符,26-5=21,对吗?非常感谢你为我提供的帮助!@ChrisKing你是对的,你可以做负->正移位转换,因为字母表循环,比如e你的26-5个例子:)很高兴我能帮忙;)