JavaScript Calculator (React)

need some help here, my calculator doesn’t work for calculations like 5x-5 or 5±5 or 5/-5

Please where should I have fix that

You’re not consistently allowing a negative number as the right hand side of a calculation, and you’re not calculating properly afaics. You need to look carefully at how you’re building up the input – I would strongly advise inputting as numbers so you can get JS to run normal calculations, but as you’re using eval you need to be a lot more careful building up the sum on input – user presses 1, add one to the display, user presses plus, add plus to display, user presses minus, hold onto the minus, user presses another number, add minus and number to display and so on.

  • if a user types + or /, then if they type it for a second time then the code just replaces the original operator with the new one. This doesn’t really matter for addition, but causes the division to be wrong.
  • if it’s a negative number divided by a positive, the calculation happens but the result is wrong (it’s always positive in your code)
  • if it’s a -, followed by a minus, that works.
  • if it’s a *, you’re immediately converting that to a decimal point regardless of what happens, so that needs fixing

Edit: I think you’re replying in the wrong thread @Coder101?

@DanCouper please and how do I fix this, I’m a beginner and this project is from freeCodeCamp so I’m trying to understand it all, is it the eval that is affecting my code?

1 Like