Feedback request for calculator and a question (tests not passing)

Hi all, just finish my react calculator, I made a calculator in the past with eval but I tryed to make this without eval and from the scartch not looking on other examples, please if you can review my code I will be glad.

And also I have a question about the logic requested for the calculator.

The thing is with this operation: 5 * - + 5. That the bundle says should be 10.

But to do that I should remove “*” and “-” when I hit “+”, thats how works the example. But is wise this logic? Shouldn’t remove the negative sign when I hit “+” that was the last sign? All calculators work that way? just asking before refactor that.

My working app here: React Calculator

Not sure I understand the question.

-10 and something - 10 is not the same thing. One is unary negation the other is a subtraction operator.

In the example you posted, it is a subtraction operator that then is overwritten by the next operator +.

I mean about the original example that is given with the project: https://codepen.io/freeCodeCamp/full/wgGVVX.

In this example when I hit the sum sign it deletes both, subtract and multiply sign, so 5 * - + 5 it becomes in 5 + 5. I don’t know if general calculators work that way. I tested a few that I have and all have different results:

Galculator from manjaro gives 25 (5*5) so takes the first operator that I hit.
On an old LG works just like the example, when I hit plus remove multiply and subtract .
edit: google gives -25.
And in another cellphone I got an error.

I’m trying the understand why and when should I remove which signs to fix my code.

Most ‘simple’ calculators I know delete previous operator after you press new one.

When you press ‘5 ±*/ 5’ only the last one is taken into account - ‘5 / 5’.

Normal behavior if you’d ask me.

Why?
I don’t know technical details, but I can see a lot of problems if done differently.

Example:

‘5 - / 5’

Okay, we substract 5 from 5, what should be divided by what after that?
We divide 0 / 0 or 5 / 0 or 0 / 5?
Or should we divide 5 by 5 first, then substract?
But sustract what? 0 - 5 or 5 - 0?

I hope that was helpful.

Only thing I know is minus/plus sign before second number.

Ex if you want to multiply 5 times -1 you press ‘5 * (-1)’ and it works
Don’t know if there are more exceptions.

Here, have a read. : )

1 Like

The thing is the demo it removes 2 not just the last operator, I still don’t understand this behaviour.

I’ve already tested around 12 different calculators. No one gives 5 * - + 5 = 10. I think the demo is wrong.

Or you avoid to input the last operator then
5 * - + 5 → 5 * -5 = -25
(like google works and mostly calculators)

Or you replace the last operator then
5 * - + 5 → 5 * + 5 = 25.

But the demo and tests converts this
5 * - + 5 → 5 + 5 = 10.
I don’t understand why this logic.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.