Javascript Calculator : How to avoid two decimals in a number?

Javascript Calculator : How to avoid two decimals in a number?
0

I need help with the test case 11 & 13. I’m unable to avoid multiple decimals in a number and 13th test case where among the multiple operators between two numbers the last operator should is to be evaluated. I wish if I could avoid multiple operators but negative sign should be possible as 5 * -5 = -25.
Javascript Calculator

What about instead of using one string to hold the input, you’d use a string to hold current number/operator and an array where you put number/operator when you move to the next number/operator?
That way it’s easy to check for the current string to have only one decimal.
And for multiple operators, you’d need to check if last element in the array is an operator, and replace it with currently entered (you’ll need additional checks for -).

There are two ways you can implement calculator input:

  1. Take a string value as a whole and evaluate it on =
  2. Select and store each number and operator in some stack(s)

If you go with the first approach then your buttons must be stateful, particularly either ‘enabled’ or ‘disabled’. I guess you just need to disable . after you have one until any of math operators entered.

Thank you @jenovs & @snigo, I had been stuck for weeks and at last I came up with a regular expression /\.\d*$/g which looks out at the end zero or more digits and a decimal point, if there is any number as .56 or just . already it would retain the current state and avoid the current decimal input.