简体   繁体   English

JS计算器无法计算两位数

[英]JS calculator won't calculate double digits

I made a simple JavaScript calculator and it works when only using single digit numbers. 我做了一个简单的JavaScript计算器,它仅在使用一位数字时才有效。 But when you try to use double digit numbers (like 12), it won't work. 但是,当您尝试使用两位数数字(例如12)时,它将不起作用。 For instance, 1+2=3 but 1+12=2 because it only calculates the first two numbers entered. 例如,1 + 2 = 3但1 + 12 = 2,因为它仅计算输入的前两个数字。

I believe this is because I have an array set up to collect the first 2 numbers pressed, and then to operate on those 2 numbers, as shown in the code below. 我相信这是因为我设置了一个数组来收集所按的前两个数字,然后对这两个数字进行运算,如下面的代码所示。 How can I fix this? 我怎样才能解决这个问题?

var res = add(operands[0], operands[1]);

My code: http://jsfiddle.net/r8X9N/ 我的代码: http : //jsfiddle.net/r8X9N/

The easiest way in my opinion 我认为最简单的方法 One way would be to keep your operands as strings until you are ready to use them. 一种方法是将操作数保留为字符串,直到准备使用它们为止。

http://jsfiddle.net/r8X9N/2/ http://jsfiddle.net/r8X9N/2/

function subtract(a,b) {
    return parseInt(a,10) - parseInt(b,10);
}


var operands = ["",""];
var operator;

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

$(".num").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        $(".screen").append(selection);
        if (operator) {
            operands[1] = operands[1] + selection;
        } else {
            operands[0] = operands[0] + selection;
        }
    });
});

Another option would be to not store the operands at all and instead take the text of the string and eval it (after you get over the fact that you're using eval) 另一个选择是根本不存储操作数,而是获取字符串的文本并进行评估(在您克服了使用eval的事实之后)

http://jsfiddle.net/r8X9N/3/ http://jsfiddle.net/r8X9N/3/

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

$(".num, .operator").each(function() {
    var selection = $(this).text();
    $(this).click(function() {
        $(".screen").append(selection);
    });
});

$(".equals").click(function() {
    $(".screen").text(function(i,text){
        return eval("(" + text + ")");
    });
});

$(".clear").click(function() {
    $(".screen").text("");
});

The plus side to this version is that it now supports multiple operators and any operator that works in javascript without any additional code (just added an error catch.) http://jsfiddle.net/r8X9N/5/ 此版本的好处是,它现在支持多个运算符,并且可以在javascript中运行的任何运算符都没有任何其他代码(只是添加了一个错误捕获。) http://jsfiddle.net/r8X9N/5/

http://jsfiddle.net/r8X9N/4/ http://jsfiddle.net/r8X9N/4/

You can also store the numbers pressed in an array, and join them when an operator or equals is pressed. 您还可以将按下的数字存储在数组中,并在按下运算符或等号时将它们合并。

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 = "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM