Javascript计算器退格第一次不工作

时间:2014-12-05 16:12:11

标签: javascript

我正在使用javascript创建计算器,并且正在尝试创建退格。我最初的想法是使用.slice函数,它起到了作用。它的工作方式是关闭一个数字,但我无法让它实际从第一个值中取出数字,如果这是有道理的。它在第一次不起作用,例如,如果我键入12,然后使用退格键,然后使数字13和13 + 6;它会拿出NaN。但是,如果我再次执行相同的过程,只要我使用清除按钮而不刷新页面,它就会起作用。我已经尝试了我能想到的一切,但是不能让它发挥作用。这是代码,对不起很多,但我认为最好把它全部放在那里。我对任何建议持开放态度,但不想使用jQuery,因为我还没有学过关于jQuery的一件事

代码:

        //Variables
        var xValue = 0;
        var xValue2 = 0;

        //Button Values
        var plusButton = 0;
        var subButton = 0;
        var timesButton = 0;
        var divideButton = 0;
        var squaredButton = 0;
        var powerButton = 0;

        //Answers
        var sum = 0;
        var difference = 0;
        var product = 0;
        var quotent = 0;
        var square = 0;
        var power = 0;

        //Functions

        function add () {
            if (plusButton >= 1) {
                xValue2 = xValue;
                xValue = "";
            }

            subButton = 0;
            timesButton = 0;
            divideButton = 0;
            squaredButton = 0;
            powerButton = 0;
            //alert(xValue);
        }

        function addFunction () {
            sum = +xValue + +xValue2;
            answer.innerHTML = sum;
        }

        function subtract () {
            if (subButton >= 1) {
                xValue2 = xValue;
                xValue = "";
            }

            plusButton = 0;
            timesButton = 0;
            divideButton = 0;
            squaredButton = 0;
            powerButton = 0;
        }

        function subtractFunction () {
            difference = +xValue2 - +xValue1;
            answer.innerHTML = difference;
        }

        function multiply () {
            if(timesButton >= 1) {
                temp = xValue2;
                xValue2 = xValue;
                xValue = "";
            }

            subButton = 0;
            plusButton = 0;
            divideButton = 0;
            squaredButton = 0;
            powerButton = 0;
        }

        function multiplyFunction () {
            product = +xValue * +xValue2;
            answer.innerHTML = product;
        }

        function divide () {
            if(divideButton >= 1) {
                temp = xValue2;
                xValue2 = xValue;
                xValue = "";
            }

            subButton = 0;
            plusButton = 0;
            timesButton = 0;
            squaredButton = 0;
            powerButton = 0;
        }

        function divideFunction () {
            var quotent = +xValue / +xValue2;
            answer.innerHTML = quotent;
        }

        function squared () {
            subButton = 0;
            plusButton = 0;
            timesButton = 0;
            divideButton = 0;
            powerButton = 0;
        }

        function squaredFunction () {
            square = Math.pow(xValue, 2);
            answer.innerHTML = square;
        }

        function powerNum () {
            if(powerButton >= 1) {
                xValue2 = xValue;
                xValue = "";
            }

            plusButton = 0;
            subButton = 0;
            timesButton = 0;
            divideButton = 0;
            squaredButton = 0;
        }

        function powerFunction () {
            var power = Math.pow(xValue2, xValue);
            answer.innerHTML = power;
        }

        function compute () {
            if(plusButton >= 1) {
                addFunction();
                xValue = sum;
            }

            else if (subButton >= 1) {
                subtractFunction();
                xValue = difference;
            }

            else if (timesButton >= 1) {
                multiplyFunction();
                xValue = product;
            }

            else if (divideButton >= 1) {
                divideFunction();
                xValue = quotent;
            }

            else if (squaredButton >= 1) {
                squaredFunction();
                xValue = square;
            }

            else if (powerButton >= 1) {
                powerFunction();
                xValue2 = power;
            }

            xValue2 = 0;
            plusButton = 0;
            subButton = 0;
            timesButton = 0;
            divideButton = 0;
            squaredButton = 0;
            powerButton = 0;
        }

        function clearScreen() {
            answer.innerHTML = "";
            xValue = 0;
            plusButton = 0;
            xValue2 = 0;
            temp = 0;
        }

        function backOne () {
            var str = answer.innerHTML;
            var res = str.slice(0, -1);
            answer.innerHTML = res;
            xValue = parseInt(res);
        }

1 个答案:

答案 0 :(得分:1)

您可以在计算器中使用当前显示值的子字符串,并将当前值替换为其子字符串。子串将是(0,长度-1)。

http://www.w3schools.com/jsref/jsref_substring.asp

编辑:应该是长度 - 1