HELP... JavaScript Calculator

i’ve got three major issues to fix

  • When the decimal element is clicked, a "." should append to the currently displayed value; two "." in one number should not be accepted.... i still have problems with the decimal input. how can i avoid a user entering 5.5.5
  • Pressing an operator immediately following "=" should start a new calculation that operates on the result of the previous evaluation... even after pressing the equal button, i should still be able to add up other value

to the first point, if you are only displaying the current number (and not numbers and operators), you can simply check if the current display contains a period. There are a few methods you could use for this, perhaps looking on the MDN for indexOf(...)

And to the second, without knowing what sort of “stack” of operations you’re using, we can’t really tell you how to operate on the last value after the equals. For my calculator, I created an array containing number-operator-number-operator-number series, and then when the enter was pressed, I reduced that down to a single value. Pressing an operator at that point simply used the answer as the first number in my new calculation stack. But “your mileage may vary” (YMMV).

Thanks, was finally able to fix it. Tried using indexOf() but didnt work so had to resort at last to regular expression.

Here is my final solution here. Thanks

2 Likes