I have a state object with the name firstDig which is as follows:-
const [firstDig, setfirstDig] = useState({
firstDigit: "",
operator: "",
secondDigit: "",
result: "",
});
I want that when a user types in the firstDigit and secondDigit, there would be only be one period(.) in both of them like it happens in the real calculator.
This is my first() function which is used to add values to firstDigit and secondDigit as the user types them.
const first = (e) => {
setdisResult(false);
if (firstDig.operator !== "") {
setfirstDig({
...firstDig,
secondDigit: `${firstDig.secondDigit}${e}`,
});
} else {
if (firstDig.firstDigit.includes(".")) {
setfirstDig({
firstDigit: firstDig.firstDigit,
});
}
setfirstDig({
...firstDig,
firstDigit: `${firstDig.firstDigit}${e}`,
});
}
};
What I have tried:
I tried creating an if statement with a check of (firstDig.firstDigit.includes(".")). If the firstDigit or secondDigit already contains a period(.) then don't add anything to the state variable object. This doesn't seem to be working.