Calculator Project for Frontend

Hey, I am working on the calculator project. I am runnng into a failing test because of decimals. Here is the error Im getting: `When the decimal element is clicked, a “.” should append to the currently displayed value; two “.” in one number should not be accepted.

An input of “5 . .0” should display 5.0: expected ‘5…0’ to equal ‘5.0’`

Here is the snippet of code im messing with to try and get it to work:

const onClick = (button) => {
    switch (button) {
      case "AC":
        setOperations(["0"]);
        break;
      case "=":
        calculateOperations();
        break;
      default:
        const newData = update(operations, { $push: [button] });
        const firstDigit = newData[0];

        if (firstDigit === "0") {
          newData.shift();
        }

        for (let i = 0; i < newData.length; i++) {
          let noDecimal;
          if (newData[i] === "." && newData[i + 1] === ".") {
            noDecimal = [...newData.slice(newData[i], newData[i + 1])];
            console.log(noDecimal);
          }
        }
        setOperations(newData);
        break;
    }
  };

hello, did you try regExp for [ . ] and then just simply replace it/them when matched??!!

Best regards.

@bappyasif thank you so much, I did end up getting it with some help. this is the final code if (numberGroups[currentNumber].includes(".") && button === ".") { return; }

1 Like