题解 | #简易计算器#

简易计算器

https://www.nowcoder.com/practice/8237298fd1354cdaabd04796280cc90e

var Calculator = {
        init: function () {
          var that = this;
          if (!that.isInited) {
            that.isInited = true;
            // 保存操作信息
            // total: Number, 总的结果
            // next: String, 下一个和 total 进行运算的数据
            // action: String, 操作符号
            that.data = { total: 0, next: "", action: "" };
            that.bindEvent();
          }
        },
        bindEvent: function () {
          var that = this;
          // 请补充代码:获取 .cal-keyboard 元素
          var keyboardEl = document.querySelector(".cal-keyboard");
          keyboardEl &&
            keyboardEl.addEventListener("click", function (event) {
              // 请补充代码:获取当前点击的dom元素
              var target = event.target;
              // 请补充代码:获取target的 data-action 值
              var action = target.getAttribute("data-action");
              // 请补充代码:获取target的内容
              var value = target.innerText;
              if (action === "num" || action === "operator") {
                that.result(value, action === "num");
              }
            });
        },
        result: function (action, isNum) {
          console.log(action, isNum);
          var that = this;
          var data = that.data;
          if (isNum) {
            data.next = data.next === "0" ? action : data.next + action;
            !data.action && (data.total = 0);
          } else if (action === "清空") {
            // 请补充代码:设置清空时的对应状态
            data.total = 0;
            data.next = "";
            data.action = "";
          } else if (action === "=") {
            if (data.next || data.action) {
              data.total = that.calculate(data.total, data.next, data.action);
              data.next = "";
              data.action = "";
            }
          } else if (!data.next) {
            data.action = action;
          } else if (data.action) {
            data.total = that.calculate(data.total, data.next, data.action);
            data.next = "";
            data.action = action;
          } else {
            data.total = +data.next || 0;
            data.next = "";
            data.action = action;
          }

          // ���补充代码:获取 .origin-value 元素
          var valEl = document.querySelector(".origin-value");
          valEl && (valEl.innerHTML = data.next || data.total || "0");
        },
        calculate: function (n1, n2, operator) {
          console.log(n1, n2);
          n1 = +n1 || 0;
          n2 = +n2 || 0;
          if (operator === "÷") {
            // 请补充代码:获取除法的结果
            let result = n1 / n2;
            if (n2 === 0) {
              result = 0;
            } else {
              if (
                result.toString().split(".")[1] &&
                result.toString().split(".")[1].length > 2
              ) {
                result = result.toFixed(2);
              }
            }
            return Number(result);
          } else if (operator === "x") {
            // 请补充代码:获取乘法的结果
            let result = n1 * n2;
            console.log(result.toString().split("."));
            if (
              result.toString().split(".")[1] &&
              result.toString().split(".")[1].length > 2
            ) {
              result = result.toFixed(2);
            }
            return Number(result);
          } else if (operator === "+") {
            // 请补充代码:获取加法的结果
            let result = n1 + n2;
            console.log(result.toString().split("."));
            if (
              result.toString().split(".")[1] &&
              result.toString().split(".")[1].length > 2
            ) {
              result = result.toFixed(2);
              while (
                result.includes(".") &&
                (result.endsWith(".") || result.endsWith("0"))
              ) {
                result = result.slice(0, -1);
              }
            }
            return Number(result);
          } else if (operator === "-") {
            // 请补充代码:获取减法的结果
            let result = n1 - n2;
            console.log(result.toString().split("."));
            if (
              result.toString().split(".")[1] &&
              result.toString().split(".")[1].length > 2
            ) {
              result = result.toFixed(2);
              while (
                result.includes(".") &&
                (result.endsWith(".") || result.endsWith("0"))
              ) {
                result = result.slice(0, -1);
              }
            }
            return Number(result);
          }
        },
      };

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务