Javascript Calculator Help - Decimal

I’ve completed 15/16 requirements for the Calculator Project but I am struggling with how to handle repeating decimal points. The pen to my project is here. Any advice?

Suppose that the current expression looks like this:

5.2+6.6+7.3

Or anything silimar… You have to somehow (using regex) select the last set of digits that is 7.3 in the current example, and check if that contains any decimal already?

If it does contain decimal already, then don’t allow anymore decimals But if the decimal doesn’t exist, then you allow the decimal.

I suggest that using the browser console, first, figure out the regex to get that last number in any valid expression like this: 5.2+6.6+7.3. Once you get that, things get a bit easier.

2 Likes

I used Regex to make sure the numbers entered followed a pattern decimals entered more than once are ignored:

$(".decimal").click(function() {
toBeEval += $(this).html();
if ((/\d*.\d*./).test(toBeEval) || (/../).test(toBeEval) ) {
toBeEval = toBeEval.slice(0,-1);
};
ReactDOM.render(toBeEval, document.getElementById(‘display’));
});