I am working on my JS Calculator project but I can’t make one part work. Well, the calculator does work but one test won’t pass. Not sure how to handle it, please help?

5 + -5 works okay. But when I use multiplication or division, the program returns wrong results. For example, 8 / - 2 or 5 * - 3.

When minus is entered, you need to check if it is a binary minus or unary minus. After seeing an operator, you’re waiting for the next operand. When you’re in the state looking for the next operand and see a minus, it is unary minus so you negate the value of the next operand before you carry out the binary operation.

You didn’t provide your code so I can make any specific suggestions. But, generally speaking, you need to keep track of the calculator state. You start from the initial state. In this state, when 0 is entered, you ignore. When any digit 1 - 9 is entered, then you’re in the left operand state to handle the number input. In this state, when any digit 0 - 9 is entered, you keep appending it to the current value. When any operator is entered, then you’ll be in another state. This is the point where you need to think about what to do if minus is entered. At this point, you’ve seen left operand and an operator like

342.02 *

When you see 1 - 9, then it is a beginning of the next operand. If you see another operator like +, then according to the spec, you forget * and change the operator to /

342.02 /

At this point, when you see a minus, what does it mean? It is possibly a beginning of entering a negative number. I say “possibly” because whether it is unary minus or not depends on what happens next. Suppose you get - and 4 5. Then it means you get -45.

In summary, you keep track of states of a calculator. For each state, you determine what to do if the next input is decimal point, operator, digit, equal, and so forth.