Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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(添加二进制)_Javascript_Binary - Fatal编程技术网

JavaScript(添加二进制)

JavaScript(添加二进制),javascript,binary,Javascript,Binary,提示如下: Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a

提示如下:

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"
Example 2:

Input: a = "1010", b = "1011"
Output: "10101"
这是我的代码:

var addBinary = function(a, b) {
    var c = (Number(a) + Number(b)).toString();
    var binaryTotal = "";
    var temp = 0;
    for (var i=c.length - 1; i>=0; i--) {
        console.log(temp, 'temp')
        console.log(c[i], 'c[i]')
        c[i] = (temp + Number(c[i])).toString();
        console.log(c[i], 'after')
        if (c[i] === '3') {
            binaryTotal = '1' + binaryTotal;
            temp = 1;
        } else if (c[i] === '2') {
            binaryTotal = '0' + binaryTotal;
            temp = 1;
        } else if (c[i] === '1') {
            binaryTotal = '1' + binaryTotal;
            temp = 0;
        } else if (c[i] === '0') {
            binaryTotal = '0' + binaryTotal;
            temp = 0;
        }
        if (temp === 1 && i === 0) {
            binaryTotal = '1' + binaryTotal;
        }
        console.log(binaryTotal, 'binaryTotal')
    }
    return binaryTotal;
};
如果我使用输入进行测试,我的输出将显示
“10”
,但我预期的是
“100”
。 我认为我的代码的这一部分有问题我不知道出了什么问题。当
I
等于
0
时,我原以为
c[1]
等于
2
,但我不明白。我试图用
parseInt
切换
Number
,结果相同。 有人能解释一下吗

这是标准输出的结果:

0 temp
2 c[i]
2 after
0 binaryTotal
1 temp
1 c[i]
1 after
10 binaryTotal

这就是懒惰的答案:

函数addBinary(a,b){
return(parseInt(a,2)+parseInt(b,2)).toString(2)
}
这是一个具有无限精度的懒惰答案:

函数addBigBinary(a,b){
return(BigInt('0b'+a)+BigInt('0b'+b)).toString(2)
}

这是懒惰的答案:

函数addBinary(a,b){
return(parseInt(a,2)+parseInt(b,2)).toString(2)
}
这是一个具有无限精度的懒惰答案:

函数addBigBinary(a,b){
return(BigInt('0b'+a)+BigInt('0b'+b)).toString(2)
}

您可以从头开始迭代字符串,并根据进位构建新数组

功能添加(a、b){
设值=[],
i=a.长度-1,
j=b.长度-1,
进位=0;
而(i>=0 | | j>=0 | |进位){
进位+=(+a[i]| | 0)+(+b[j]| | 0);
值。取消移位(进位%2);
进位>>=1;//或数学楼层(进位/2)
我--;
j--;
}
返回值。join(“”);
}
控制台日志(添加('1','111111');

日志(添加('111111','1')您可以从头开始迭代字符串,并根据进位构建新数组

功能添加(a、b){
设值=[],
i=a.长度-1,
j=b.长度-1,
进位=0;
而(i>=0 | | j>=0 | |进位){
进位+=(+a[i]| | 0)+(+b[j]| | 0);
值。取消移位(进位%2);
进位>>=1;//或数学楼层(进位/2)
我--;
j--;
}
返回值。join(“”);
}
控制台日志(添加('1','111111');

日志(添加('111111','1')
我假设您不允许只将字符串解析为数字或bigint,添加它们并将结果转换回二进制字符串?当您说
将字符串解析为数字时,您的意思是
parseInt
?我还没有学过大整数,但很高兴知道还有另外一种选择,谢谢。如果我想知道为什么我的代码不起作用,你能识别我的错误吗?是的,我的意思是
parseInt
,尽管这不是唯一的方法(参见我的答案)。我假设你不允许只将字符串解析成数字或大整数,添加它们并将结果转换回二进制字符串?当您说
将字符串解析为数字时,您的意思是
parseInt
?我还没有学过大整数,但很高兴知道还有另外一种选择,谢谢。如果我想知道为什么我的代码不起作用,你能识别我的错误吗?是的,我的意思是
parseInt
,尽管这不是唯一的方法(见我的答案)。