Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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
Javascript JS计算器赢得';不计算两位数_Javascript_Jquery - Fatal编程技术网

Javascript JS计算器赢得';不计算两位数

Javascript JS计算器赢得';不计算两位数,javascript,jquery,Javascript,Jquery,我制作了一个简单的JavaScript计算器,当只使用一位数时,它就可以工作了。但当你尝试使用两位数(比如12)时,它就行不通了。例如,1+2=3,但1+12=2,因为它只计算输入的前两个数字 我相信这是因为我设置了一个数组来收集按下的前两个数字,然后对这两个数字进行操作,如下面的代码所示。我怎样才能解决这个问题 var res = add(operands[0], operands[1]); 我的代码:在我看来,最简单的方法之一是将操作数保持为字符串,直到您准备好使用它们为止 另一种选择

我制作了一个简单的JavaScript计算器,当只使用一位数时,它就可以工作了。但当你尝试使用两位数(比如12)时,它就行不通了。例如,1+2=3,但1+12=2,因为它只计算输入的前两个数字

我相信这是因为我设置了一个数组来收集按下的前两个数字,然后对这两个数字进行操作,如下面的代码所示。我怎样才能解决这个问题

var res = add(operands[0], operands[1]);
我的代码:

在我看来,最简单的方法之一是将操作数保持为字符串,直到您准备好使用它们为止


另一种选择是根本不存储操作数,而是获取字符串的文本并对其求值(在您忘记使用eval的事实后)

这个版本的优点是,它现在支持多个运算符以及在javascript中工作的任何运算符,而无需任何附加代码(只是添加了一个错误捕获)。

我认为最简单的方法之一是将操作数保留为字符串,直到您准备好使用它们为止


另一种选择是根本不存储操作数,而是获取字符串的文本并对其求值(在您忘记使用eval的事实后)

这个版本的优点是,它现在支持多个操作符和任何在javascript中工作的操作符,而不需要任何额外的代码(只是添加了一个错误捕获)。

您还可以将按下的数字存储在数组中,并在按下运算符或等于时将其合并

var num = [];

$(".screen").text("");

$(".num").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        $(".screen").append(selection);
        num.push(parseInt(selection));
        //operands.push(parseInt(selection));
    });
});

$(".operator").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        operator = selection;
        operands.push(parseInt(num.join('')));
        num = [];
        $(".screen").append(operator);
    });
});

$(".equals").click(function() {
    operands.push(parseInt(num.join('')));
    num = [];
    switch (operator) {
        case "+":
            var res = add(operands[0], operands[1]);
            $(".screen").text("").append(res);
            operands = [];
            operator = "";

您还可以将按下的数字存储在数组中,并在按下运算符或等于时将其合并

var num = [];

$(".screen").text("");

$(".num").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        $(".screen").append(selection);
        num.push(parseInt(selection));
        //operands.push(parseInt(selection));
    });
});

$(".operator").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        operator = selection;
        operands.push(parseInt(num.join('')));
        num = [];
        $(".screen").append(operator);
    });
});

$(".equals").click(function() {
    operands.push(parseInt(num.join('')));
    num = [];
    switch (operator) {
        case "+":
            var res = add(operands[0], operands[1]);
            $(".screen").text("").append(res);
            operands = [];
            operator = "";

问题在于如何生成操作数数组。不是1+12创建[1,1,2],而是需要它来创建[1,12]。需要在按运算符键时将项推入操作数数组,而不是每次按num键时。这会导致@KevinB指出的错误数组。问题在于如何生成操作数数组。不是1+12创建[1,1,2],而是需要它来创建[1,12]。需要在按运算符键时将项推入操作数数组,而不是每次按num键时。这会导致@KevinB指出的错误数组。
var num = [];

$(".screen").text("");

$(".num").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        $(".screen").append(selection);
        num.push(parseInt(selection));
        //operands.push(parseInt(selection));
    });
});

$(".operator").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        operator = selection;
        operands.push(parseInt(num.join('')));
        num = [];
        $(".screen").append(operator);
    });
});

$(".equals").click(function() {
    operands.push(parseInt(num.join('')));
    num = [];
    switch (operator) {
        case "+":
            var res = add(operands[0], operands[1]);
            $(".screen").text("").append(res);
            operands = [];
            operator = "";