Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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. 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()
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading