Failing tests that seem to pass - calculator

Hi everyone - I’ve having a confusing time with the FCC tests on the Javascript Calculator project. The automatic tests fail on 8/16 which seem to get the correct outputs when I enter the examples manually. Please could someone have a look? Thanks very much!

Tests’ comparison seem to be always the first digit it enters to the calculator, for example:

expected ‘1’ to equal ‘123’

“5 . . 0” should display 5.0 : expected ‘5’ to equal ‘5.0’

“5 * - 5” = should produce an output of “-25” : expected ‘5’ to equal ‘-25’

“5 - 2 = / 2 =” should produce an output of “1.5” : expected ‘5’ to equal ‘1.5’

That might show tests happen before inside of the h1 element changes, that explains the passing test for starting with extra zeros.

There is obviously a problem with the tests since your calculator works fine, but you could try to handle click events with some other method instead of using lastPress object.

1 Like
  1. I can enter more than one decimal point like 3.2.1. It should be 3.21, ignoring the second point.
  2. When I enter point about =, it won’t reset. For example, if I enter 3 + 2 = , and then point and 3, it won’t be 0.3, but 53.
  3. (I think this is related to 2.) From the initial state, pressing of a decimal point is ignored. Pressing . 5, should produce 0.5. (Actually, I’m not sure the auto tests consider this situation).

Once you have an error, some of the explanations for test failure could not be meaningful. For example, one test (#14) says The sequence “5 - 2 = / 2 =” should produce an output of “1.5” But your code produces the correct result for this case. I suggest you ignore them and fix the known errors (I pointed out a few) first. Once you fixed the known errors, you could pass all other tests.

1 Like

Thank you for having a look at the project @ozanbaskan, I agree it seems like there is a problem with several tests.

Thank you @twotani for finding these bugs! I think I have solved the ones you mentioned now but unfortunately it’s only passing 8/16 still. As you mentioned, several of the examples it gives in the error messages seem to produce the correct output when you try them manually yourself.

It looks like your code is working. I don’t know React well enough so I’m not sure, but there must be some kind of timing issue for updating a value. For instance, errors #7 and #8 are complaining that ‘display’ is not showing correct value. When I press AC, the display shows 0. I guess somehow before your


takes effect at

    <h1 id="display">{displayValue}</h1>

The tester is looking at the value of this h1 tag before the value changes to 0?

Thanks for trying to help @twotani, maybe that’s what is going on behind the scenes but considering React is recommended for this challenge I think it suggests the tests have a problem. I’ll try to escalate it to the moderators.

Well, I implemented a React version of plain JS calculator. My original JS calculator passed all 16 tests okay. However, my React version failed three tests. But as far as I can tell, the React version seems to be working correctly. One interesting behavior is that three fail cases are repeated twice. Like this

10. When inputting numbers, my calculator should not allow a number to begin with multiple zeros.
Script error. (:0)
. . . 
11. When inputting numbers, my calculator should not allow a number to begin with multiple zeros.

Of course, my React calculator won’t allow entering multiple zeroes!?! I’ll troubleshoot some more and see if I can find something new and interesting.

Okay, my React calculator passed all 16 tests. It is just one error. Originally I had this statement

this.changeState(calculator.state, Move.ZE)

This is wrong. But I cannot detect anything wrong because the code won’t crash or anything. I saw the correct display.

The expression calculator.state is valid in my JS calculator. calculator is an object to keep track of various properties of a calculator. The property state is the state of a calculator, as I’m using a finite state machine. Since state has specific meaning in React, I rename it status. The statement in React should be

this.changeState(this.state.status, Move.ZE)

I changed correctly everywhere except in one method for handling the entering of 0.

After I made this one change, the code passed all 16 tests. So, I suspect your error must be something like this. One error hiding somewhere.

I can provide a URL to my JS and React calculators if you are interested in seeing them.

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