React calculator: limiting input zeroes

React calculator: limiting input zeroes
0

#1

This code works as intended. However, whenever I add a number after a zero, both numbers get displayed (eg, if I press 0 and 4, the display shows 04). I thought about changing this with a RegEx, but I wouldn´t know how to implement it to be honest.


#2

This is something you can check for inside your handleClick event.

For example, you can check if value is 0 then instead of concatenating the value of 4, you instead replace the value with 4. You can also add a “if value is zero and pressed is zero do nothing” and so on and so forth.

Does that make sense?


#3

Man, I feel so stupid for not thinking this one up myself. Thanks a bunch!


#4

Sorry, it´s me again. I tried for hours, but couldn´t crack it. Eventually I gave up, and went to Stackoverflow. They told me to do this:

Which sort of works, but still allows “01” and so on. Don´t know how to fix that. Tried wrapping it around “Number” but that brings only more additional problems (like not allowing + and so on).