I have found some of the test cases used for the calculator project (https://www.freecodecamp.org/learn/front-end-libraries/front-end-libraries-projects/build-a-javascript-calculator) to be contradictory.
I am specifically talking about test case #9
9. In any order, I should be able to add, subtract, multiply and divide a chain of numbers of any length, and when I hit "=", the correct result should be shown in the element with the id of "display"
and test case #11
If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.
The test script for #13
5 * + - 5 = 10
I have written my project using equation notation - in that values are calculated cumulatively as they are entered, and so produces a result for #13 of -25.
I have checked, my calculators logic against Googles calculator which appears to use a similar approach.
Don’t mean for this post to sound negative, just something I wanted to highlight as I have spent a considerable amount of time up to this point getting a calculator to work (and it does work as it should - in line with other calculators I’ve looked at) - just not in line with this one test.
Shouldn’t it be zero? 5 + (-5) = 0
How are you getting -25 if you are skipping the multiplication and doing addition instead?
I also do not see that example in the test, I see these.
'The sequence "5 * - 5" = should produce an output of "-25" '
'The sequence "5 * - + 5" = should produce an output of "10"'
'The sequence "5 + + 5" = should produce an output of "10" '
That is my test case,
So may not of made myself clear in the first post:
The test case doesn’t make sense mathematically 5 * - + 5 = 25
Mathematical rule is subtracting a positive is the same as adding a negative.
From Symbolab:
So although I understand the test case wants to ignore the middle subtraction symbol but surely it would be better to have a test case that checks that you haven’t entered a formula in that format. Rather than when you enter that format, it equals a number which isn’t mathematically correct.
IMO should check if
input: 5 * - + 5
Output: 5*5 = 25 (same as saying: 5 * (+5))
But that is the requirement. Skip the operation if followed by another operation unless the operator is minus, in which case, make the number that follows a negative number.
If you put what you say into eval
, then yes you would get -25
eval('5 * - + 5')
-25
But that is not what the requirement says to do.
Edit:
If you want you can open an issue on one of the repos