题解 | #简易计算器#

简易计算器

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);
          }
        },
      };

全部评论

相关推荐

10-25 00:32
香梨想要offer:感觉考研以后好好学 后面能乱杀,目前这简历有点难
点赞 评论 收藏
分享
沉淀一会:1.同学你面试评价不错,概率很大,请耐心等待; 2.你的排名比较靠前,不要担心,耐心等待; 3.问题不大,正在审批,不要着急签其他公司,等等我们! 4.预计9月中下旬,安心过节; 5.下周会有结果,请耐心等待下; 6.可能国庆节前后,一有结果我马上通知你; 7.预计10月中旬,再坚持一下; 8.正在走流程,就这两天了; 9.同学,结果我也不知道,你如果查到了也告诉我一声; 10.同学你出线不明朗,建议签其他公司保底! 11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务