There needs some help with test case 11 & 13 which needs avoidment of multiple decimals in a number and 13th test case where continuous inputs of multiple operators between two numbers the last operator is to be evaluated with one case of negative sign possibility 5 * -5.
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:
- Take a string value as a whole and evaluate it on
=
- 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.