【问题标题】:Create a MR and MC in a javascript calculator在 javascript 计算器中创建 MR 和 MC
【发布时间】:2016-10-28 22:51:26
【问题描述】:

理想情况下,我希望我的小项目具有记忆功能 M-、M+、MR 和 MC。 我正在考虑单独的函数和变量来保存 M- 和 M+。 这是一种正常的方法还是有更好的方法?

知道我的脚本可能有什么问题吗?有什么问题吗?

数字显示ID是实际的计算器屏幕

代码是:

$(document).ready(function(){
    var display = "";
    var operators = ["/", "*", "-", "+"];
    var decimalAdded = false;

    $("button").click(function() {
        var key = $(this).text();

      //update screen by adding display string to screen with maximum 19 numbers viewable
        function updateDisplay() {
            if (display.length > 19) {
              $("#number-display").html(display.substr(display.length - 19, display.length));
            } else {
              $("#number-display").html(display.substr(0, 19));
            }
        }
      //clear all entries by resetting display and variables
        if (key === "AC" || key === "ON" || key === "MC") {
            decimalAdded = false;
            display = "";
            $("#number-display").html("0");
        }

        else if (key === "OFF") {
            decimalAdded = false;
            display = "";
            $("#number-display").html("");
        }


      //clear previous character and reset decimal bool if last character is decimal
        else if (key === "CE") {
            if (display.substr(display.length - 1, display.length) === ".") {
                decimalAdded = false;
            }
            display = display.substr(0, display.length - 1);
            updateDisplay();
        }

      //add key to display if key is a number
        else if (!isNaN(key)) {
            display += key;
            updateDisplay();
        }
      //check that . is the first in the number before adding and add 0. or just .  
        else if (key === ".") {
            if (!decimalAdded) {
                if(display > 0){
                    display += key;
                }
                else {
                    display += "0" + key;
                }
                decimalAdded = true;
                updateDisplay();
            }
        }
        //if key is basic operator, check that the last input was a number before inputting
        else if (operators.indexOf(key) > -1) {
            decimalAdded = false;
            //first input is a number
            if (display.length > 0 && !isNaN(display.substr(display.length - 1, display.length))) {
                display += key;
                updateDisplay();
            }
        // allow minus sign as first input
            else if (display.length === 0 && key === "-") {
                display += key;
                updateDisplay();
            }

        }
        // calculate square root of number

        else if ( $(this).id === "sqrt") {

                var tempStore = display.html();
                $("#number-display").html(eval(Math.sqrt(tempStore)));
                decimalAdded = false;
        }

        // change sign of number

        else if ($(this).id === "plusmn") {
            var newNum = display * -1;
            $("#number-display").html(newNum);
        }

        // create memory plus and minus and calculate MR
        else if (key === "M-") {

        }
        else if (key === "M+") {

        }

        // percentage function

        else if (key === "%"){

        }

        else if (key == "=") {
            //if last input is a decimal or operator, remove from display
            if (isNaN(display.substr(display.length - 1, display.length))) {
                display = display.substr(0, display.length - 1);
            }
            var calc = display;
            calc = eval(calc);
            display = String(calc);
            if (display.indexOf('.')) {
                decimalAdded = true;
            } else {
                decimalAdded = false;
            }
            $("#number-display").html(display);
        }
    });});

【问题讨论】:

    标签: javascript function memory calculator


    【解决方案1】:

    另一种选择是switch 语句,类似于:

    switch (key) {
      case "M-":
        // do stuff
        break;
      case "M+":
        // do stuff
        break;
      case "%":
        // do stuff
        break;
      case "=":
        // do stuff
        break;
    }
    

    有关 MDN 的更多文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

    【讨论】:

    • 谢谢保罗,我会调查的。它的那种 if/else 类型的结果不是吗?如果我为 M= 和 M- 添加变量并为 & 创建一个函数,那么它基本上解决了:P
    • 是的,它就像 if/else,只是它更具可读性并且您可以轻松添加更多案例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    相关资源
    最近更新 更多