简体   繁体   English

更新 JS 计算器的显示

[英]Updating display on JS calculator

I want to have my calculator to display "0" when cleared or no other numbers have been entered but when I start adding numbers I want the 0 to be replaced.我想让我的计算器在清除或未输入其他数字时显示“0”,但当我开始添加数字时,我希望替换 0。 Currently when I enter any number it replaces the number on the display to the number entered.目前,当我输入任何数字时,它会将显示屏上的数字替换为输入的数字。

this.currentDisplay = "0"

numberData(number) {
        if (number === "." && this.currentDisplay.includes("."))
        return
        if (this.currentDisplay = "0") {
        this.currentDisplay = this.currentDisplay.toString().replace("0", number.toString())
        }
        else {
            this.currentDisplay = this.currentDisplay.toString() + number.toString()
        }
    }

You have an error on the this condition:您在这种情况下有错误:

if (this.currentDisplay == "0") {
   ...
}

In your code you are assigning this.currentDisplay = 0 , you should compare with == or better === to compare the types of the variables too.在您分配的代码中this.currentDisplay = 0 ,您应该与==或更好的===进行比较以比较变量的类型。

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

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